7

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.................

4
  • 2
    You're using the same ID on two elements. Only use an ID once. Commented May 27, 2011 at 18:24
  • @tylermwashbum using id once.......... Commented May 27, 2011 at 18:32
  • There are two labels with the ID 1. BTW, you should not use IDs starting with a number. Commented May 27, 2011 at 18:36
  • You create a new label with the same id as the one clicked. (d.id=me.id;) And as bazmegakapa mention, ID's should always start with a letter. Commented May 27, 2011 at 18:44

3 Answers 3

18

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.

Sign up to request clarification or add additional context in comments.

6 Comments

still when i click that label after adding this property. it is not calling the function.........
@maerics. the onclick event is still not working. what may be the problem...??
@thuk: please edit your question with details about what you are trying to accomplish and what specifically happens when you run your existing code, this way others can easily see the problem. Just a guess, the argument "me" to the function looks suspicious because it will duplicate ids, which should be unique...
@thuk You can easily create a jsFiddle on jsfiddle.net to illustrate your problem even better.
@maerics finally ur code worked for me. very thanks. i have done a silly spelling mistake.... thanks ......
|
6
d.setAttribute('onclick', 'alert(\'hello\');');

Comments

0

For creating an attribute to a HTML tag, sometimes we have to add this:

yourTag.src
yourTag.src = 'http://lolxd.com/404.png'

But there are special attributes, and them have diferents ways for editing:

yourTag.classList
yourTag.className

And there is the onclick attribute, wichwe can use it like this:

// The first way
obj.onclick = function () { alert('lalala') }
// With the Event Listener
obj.addEventListener('click', function () { alert('lalala') }, false)
// Or, a text-render way
obj.setAttribute('onclick', 'alert(`lalala`)')

I recomend you the Event Listener way, but try all :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.