javascript - Jquery not calling function on click of dynamically added button -
this question has answer here:
i have function , code:
function findmember(str) { // console.log('search: ' + str); $.post("includes/search.php", { search: str, }, function(data, status) { // console.log(data); $('#creatorsearchmain').html(data); }); } $('#creatorsearchinput').keyup(function() { var target = $('#creatorsearchinput'); findmember(target.val()); //remove , uncomment other if want wait before request }); this selects text input , uses .post query database search user name searched. .post returns html code added div.
in html returned there button:
<button class="followbutton " type="submit" value="1">follow</button> when button clicked runs javascript function.
this works except when try , click button after have used .html add html returned div.
to clarify, button works when page first loaded , there when put there new results not seem picked javascript.
this using activate onclick button function:
$('.followbutton').on("click", function() { #code in here }); thanks in advance!
your button being added dynamically, use event delegation using on() adding click handlers it,
$("#creatorsearchmain").on("click",".followbutton", function() { #code in here }); *the selector before on(), $("#creatorsearchmain") here, should static element.
Comments
Post a Comment