Let’s try something… go to a page with jQuery enabled (such as this one), and run the following code in your Javascript debugger console (such as Firebug):
var button =
$('<button>Click me</button>')
.click(function(){alert('Clicked!')})
.appendTo('body')
In case you were wondering, this creates a brand new button, causes it to display a “Clicked!” message box when it’s clicked, and appends it to the document you are viewing.
Click on the button that just appeared : the message box appears. Not very surprising.
Now, run the following code on the same page :
$('body').html('');
button.appendTo('body')
As expected, everything on the page, including the button, disappears. However, the button is still referenced by the button variable, so it sticks around and we can append it back to the document. And indeed, it does appear on the page.
Click on the button again. This time, no message box appears.
I honestly have no idea why.
Hi. I'm Victor Nicollet,
Recent Comments