2

I'm dynamically creating a div. I'd like to add a onClick() event to it.

How do I add an onClick as in <div class="something" id="btnHome" onClick="return true"> to this?

abc.append(
    $('<div />', { class: 'something', id: 'btnHome' })
); 

EDIT:

I'm looking for an answer something like this

$('<div />', { class: 'something', id: 'btnHome' onClick: 'return true' })
4
  • ....}).on("click",function() {return true;}); or delegate like in the answer Commented Apr 19, 2015 at 9:50
  • @mplungjan: Nope. I'm looking for an answer like this $('<div />', { class: 'something', id: 'btnHome' onClick: 'return true' }) Commented Apr 19, 2015 at 9:52
  • Not a good idea. USe jQUery now you have it. And why is it better for you to have the handler inside the brackets than outside? Tell us more WHY you want this Commented Apr 19, 2015 at 9:52
  • 2
    Because adding event handlers are better than using attributes to execute code. The best solution is to add it with delegation as shown in @Tushar s answer Commented Apr 19, 2015 at 9:55

3 Answers 3

10

Use event delegation:

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

For dynamically added elements use parent to add event to the added elements.

$(staticParentSelector).on('click', '.something', function () {
    // Event Handler Code here
});

You can also use document, but this costs performance

$(document).on('click', '.something', function () {
    // Event Handler Code here
});

see jQuery Docs for on

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

1 Comment

I should place this in the second click part right?
8

Event delegation is the right way to do it and @Tushar has the answer for you. But, if you were after something like this:

$('<div />', { class: 'something', id: 'btnHome' onClick: 'return true' })

Then, you may do:

$('<div/>', { 
    'text': 'new',
    'class': 'something',
    'id': 'btnHome'
}).on({
    'click': function() { alert ("clicked") }
});

Demo@Fiddle

As squint suggests in one of the comments below, you could also use click as an attribue just like text or id as the following.

$('<div/>', { 
    'text': 'new',
    'class': 'something',
    'id': 'btnHome',
    'click': function() { alert ("clicked") }
});

5 Comments

Problem with this approach is that, you are adding event listener to each element created. Using event delegation, the event is only attached once for the parent.
@Tushar: That's not an actual problem. People just like to go around claiming it is.
@Tushar: Why? It does nothing to demonstrate the problem you claim, and in fact it puts delegate handlers on the document, which is arguably worse.
@Ishettyl: FYI, you can skip the on() and add the click handler as one of the attributes when creating the element. $("<div>", {click: function() {...}, text: 'new', ...})
This has the huge advantage that the inserted element doesn't need to have any unique identifier which you need for event delegation. Thanks for this approach, it was exactly what I was looking for.
0

You can use delegated events so add to your jquery code:

$(document).on('click','#btnhome',function(){'do something here'});

You can use document or any other parent element that is already in the DOM

1 Comment

I have also read the comments and agree with what they say. It is not a good idea to add code this way. Use jquery instead.

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.