I have a link inside of a div element. The link has a click listener.
$(document).ready(function() {
$('#theLink').click(function() {
// do stuff
});
})
<div id="myElement"><a class="myLink" id="theLink">Click here</a></div>
At some point in my application, I have to clear the div elements contents, and then have to re-write them again later. When I rewrite the link using .html(''), I lose the click listener and the link doesn't work.
$('#myElement').html(''); // clear element
$('#myElement').html('<a class="myLink" id="theLink">Click here</a>');
Do I need to add a new listener to get this link working again?
.live()or.on()instead of.click()hide/showit? It doesn't make sense to continuously destroy and recreate the same element, even if methods likeon()orlive()let you.