0

My question concerns cloning and cloning events.

I have this button:

<button type="button" class="btn btn-inverse test">test</button>

In my JS:

//create a clone of my button at the start so any later changes to the btn do not effect future clones
var test = $(".test").clone(true);  

$(".test").click(function() {
    alert('a');
});

test.clone(true).insertBefore('#addQuestion');

The above clones the button but the event no longer works, where am I going wrong?

4

1 に答える 1

4

That's because you are cloning the element before adding listener.

var test = $(".test").click(function() {
               alert('a');
           }).clone(true); // .insertBefore('#addQuestion')

http://jsfiddle.net/j7XR9/

于 2013-03-06T18:25:30.997 に答える