-1

Having a problem , can't explain because not sure what is happening. Just can only simplify the problem in this example

What hapening

I'm putting the element in a array temporally, then when I put again doesn't have the click event.

How to test

  1. Use "click me" button, will show the alert
  2. detach, will change place
  3. will appear the button "click me" in another place
  4. click on the "click me" button , doesn't work
Ricardo Rodrigues
  • 2,633
  • 5
  • 29
  • 31

2 Answers2

2

You should pass true to clone method in order to preserve the events.

Take a look http://jsfiddle.net/7ZBtZ/2/

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • You're solution it's very nice for the question. But next time have to read API documentation before. My main problem was deep copy needed to ue clone(true,true). Thanks – Ricardo Rodrigues Aug 10 '11 at 09:43
2

@Shankar's answer is fundamentally correct (+1), but there is a better way to do this. Right now, you:

  1. Clone the element, then
  2. Remove it, then
  3. Detach it (which is redundant since the element was just removed), then
  4. Append it

Instead, you can just .detach() the element, which

is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

It would work something like this:

var $div = $('#div').detach();
hidden.push($div);
$('#click').remove();
hidden.pop().detach().appendTo('#div1');

Demo: http://jsfiddle.net/mattball/swBfj/

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710