Thomas Depole Thomas Depole - 1 year ago 86
jQuery Question

Jquery .click() doesn't work after html has been changed

It's pretty basic, I have a button, and when you click it, it changes. But after it changes the

event doesn't work on it, but works on divs that are already on the page

/* this captures html text and replaces it with textbox */
var oldBio = $('.bioText').html();
$('.bioText').replaceWith(' <textarea name="newBio" rows="6" cols="30" > ' + text + ' </textarea>');
/* this switches button */
$('#editBioButton').replaceWith(' <div id="saveBioText"> Save Changes </div> ');
/* this div won't react when clicked */


Answer Source

I ran into the same problem before. Turns out jQuery loses all bindings on elements that are added after loading. You should use the delegate method

$("#editBioButton").delegate("#saveBioText", 'click', function(){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download