Thursday, May 17, 2018

Jquery event not firing for a button created in javascript with innerHTML





I search around the web but cant do this work..



Im creating a dialog in a js.file . In this js i have a function that creates a button, but with innerHTML...



    var divButtons = document.getElementById('buttons_insert1');    

var buttons_insert = "";
divButtons.innerHTML = divButtons.innerHTML + buttons_insert;


and in the index.html i have a jquery function to get the click event.



$("#okcad1").click(function() 
{
alert('hello world');
}



but this is not working..the event dont fire...



Someone could help me?



Regards!



Rafael S.


Answer




Use event delegation



$(document).on("click" , "#buttons_insert1 #okcad1" , function() 
{
alert('hello world');
});

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...