Thomas Depole Thomas Depole - 1 month ago 8
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

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

$('#editBioButton').click(function(){
/* 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 */
$('#saveBioText').click(function(){
alert('itowrks');
});

});

Answer

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(){
  alert("this.");
});