jQuery clone( true ) not working with dynamic elements

Posted by elclanrs on Stack Overflow See other posts from Stack Overflow or by elclanrs
Published on 2012-10-31T04:57:21Z Indexed on 2012/10/31 5:00 UTC
Read the original article Hit count: 147

Filed under:
|
|

Take the following example:

$.fn.foo = function() {

    var $input = $('<input type="text"/>');
    var $button_one = $('<button>One</button>');
    var $button_two = $('<button>Two</button>');

    $button_one.click(function(){
        $input.val('hey');
    });

    $button_two.click(function(){
        $input.replaceWith( $input.val('').clone( true ) );
    });

    this.append($input, $button_one, $button_two);


};

Check the demo: http://jsbin.com/ezexah/1/edit

Now click "one" and you should see "hey" in the input. Next click "two" and then click "one" again and it doesn't work. Even using the true option in clone to copy all events it still does not work. Any ideas?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery