Monday, 9 September 2013

onclick does not fire in a link that is created by clicking another link

onclick does not fire in a link that is created by clicking another link

Javascript function writelink creates a link that fires function hiya. It
works when I define and invoke writelink inside script. But, I want the
body to contain another link that calls writelink to create a link. This
final link fails to fire (but works if I replace hiya with alert). I am a
total novice, so I hope it is something obvious. The main idea is that I
have to create some links from code, and these have to fire to execute
some more code. (If you want me to do this a totally different way, please
try to give me a complete example.)
<HTML>
<HEAD>
<script>
function hiya (num){
alert("hiya " + num);
}
function writelink (num){
var htmlstr = "<a href=\"#\" onclick=\"hiya(" + num + ");return
false;\">link" + num + "</a>";
document.write(htmlstr);
}
writelink(1);
</script>
</HEAD>
<BODY>
<br>
<a href="#" onclick="writelink(3);return false;">link2</a>;
<br>
</HTML>

No comments:

Post a Comment