Adding onClick() dynamically not working, Why ?

P

Prince of Code

Hey all,

I m trying to add onClick() event dynamically using DOM Method . But
why the event is not able to be triggered. Also I dont see it in the
DOM Source. Pls find the code for refrence.

Your explanation would be greatly useful.

Thanks
Prince

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Sample Page </TITLE>
</HEAD>

<BODY>

</BODY>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function hellofn()
{
alert("Hello World !");
}

atag = document.createElement("a");
atag.innerHTML = "click this link";
// Why is the I am not able to call the onclick event
handler ??????????????
atag.onClick="hellofn();"
atag.href = "#";
document.body.appendChild(atag);
</SCRIPT>

</HTML>
 
A

Amrit Ranjan

Hey all,

I m trying to add onClick() event dynamically using DOM Method . But
why the event is not able to be triggered. Also I dont see it in the
DOM Source. Pls find the code for refrence.

Your explanation would be greatly useful.

Thanks
Prince

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Sample Page </TITLE>
</HEAD>

<BODY>

</BODY>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function hellofn()
{
alert("Hello World !");

}

atag = document.createElement("a");
atag.innerHTML = "click this link";
// Why is the I am not able to call the onclick event
handler ??????????????
atag.onClick="hellofn();"
atag.href = "#";
document.body.appendChild(atag);
</SCRIPT>

</HTML>

atag.onclick= hellofn;
will work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top