I’m creating and removing HTML from inside a div with jQuery (shopping cart, adding/removing items). I need to access the .click() event of a link inside this div, but it only works when the page first loads – after that link is removed then re-added it doesn’t respond to any jQuery events.
I’ve tried functions both inside and outside of $j(document).ready(function() {}. How can I make events work on this link again after re-creation?
Use
.delegate()instead of.click()(which is short-hand for.bind('click')):Source: http://api.jquery.com/delegate/
The
<root-element>can be thedocumentelement or if you know an element that is always present that is a descendant of thedocumentelement it is best to use that element.