4

I'm dynamically adding few elements to DOM. And I want to manipulate these element behavior using .on() function of jQuery. But somehow DOM is not firing .on() event for dynamically added elements.

Here is JS Fiddle that shows the example - http://jsfiddle.net/jYQ2D/1/

When I click on "Dynamically Added in HTML" button, it should show alert message but it is not showing.

Any idea why?

asked Jan 26, 2013 at 2:10

1 Answer 1

11

Delegation via .on doesn't work that way. You have to call .on on the element and then use the second argument as the delegation selector:

$(document).on("click", "#dyna", function() { 

http://jsfiddle.net/jYQ2D/2/

Documentation. Also be aware that there can be only one #dyna element.

answered Jan 26, 2013 at 2:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.