Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  40] [ 6]  / answers: 1 / hits: 30347  / 13 Years ago, fri, may 27, 2011, 12:00:00

I have created a label element. I need to add onclick event to that...



function a(me) {
var d=document.createElement(label);
d.id=me.id;
d.onClick=a(10);
d.innerHTML=welcome;
document.body.appendChild(d);
}


HTML:



<label id=1 onclick=a(this)>aa</label>
<label id=2 onclick=a(this)>bb</label>
<label id=3 onclick=a(this)>aa</label>


actually what happens is when i click the any of three labels in html. another label is created and displays welcome. now when i click the newly created label welcome it does not display anything...... that is the onclick event added to newly created label is not working ....... any suggestion.................


More From » javascript

 Answers
250

You need to set d.onclick=function(){a(1);};, note that the case matters here (not onClick).



[Edit]



Based on your comments and updated questions I've created a jsFiddle to demonstrate how you might turn your code into something that works.


[#92014] Thursday, May 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deiong

Total Points: 15
Total Questions: 103
Total Answers: 99

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
;