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

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -