Thursday 17 May 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...