0

I am trying to add a button dynamically - the first button works but not the others. I was previously using the live function, but jQuery 1.9.1 removed that function. What should I do to get the newly added buttons to work?

HTML Code:

<div class="take">
    <p>this is the para</p>
    <button class="test">copy</button>
</div>

jQuery Code:

$('.test').on('click', function() {
    var a = $(this).parent();
    new.clone().insertAfter(a);
})

Here is the jsfiddle.

8
  • 2
    The most asked question here on SO. Have you read the DOCS about .on() method ? it's all there! Commented Apr 9, 2013 at 14:54
  • using .on() is the new way to go Commented Apr 9, 2013 at 14:55
  • Use .on(), which is the preferred way to do delegation now. Commented Apr 9, 2013 at 14:55
  • Have a read at api.jquery.com/on/#direct-and-delegated-events Commented Apr 9, 2013 at 14:55
  • 1
    Cloning with events solves OP's immediate problem, but the question was how to update the code now that live is deprecated (so delegation is implied). Commented Apr 9, 2013 at 15:01

3 Answers 3

7

Passing true in clone will solve your problem - jsfiddle

$('.test').on('click', function() {
    var o = $(this).parent();
    o.clone(true).insertAfter(o);        
})

Note: new is a keyword in JavaScript so don't use it as a variable.

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

1 Comment

To give context, passing true to .clone() makes it a deep copy (with events). That's why re-adding it works (though doesn't actually rely on using .on() correctly/as intended).
2
$(document).on('click', '.test', function() {
    var o = $(this).parent();
    o.clone().insertAfter(o);   
});

Instead of document use rather a non-dynamic parent ID selector

http://jsfiddle.net/uk6Bh/1/

To visualize:

$('#non-dynamic-parent').on('event', '.dynamic-delegated-element', function() {

http://api.jquery.com/on/ says:

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

Comments

1

You want to use .on() (demo):

$(document).on('click', '.test', function() {
    var o = $(this).parent();
    o.clone().insertAfter(o);
})

2 Comments

why go as far as document if you can find a closer static parent? thats a lot of traversing no?
It's still extremely cheap (click events are rare, bubbling is fast). Also, no markup further up the DOM tree was provided to incorporate in an answer.

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.