Howzieky Howzieky - 11 months ago 36
HTML Question

Adding to innerHTML of document.body causes jQuery .on() to not find its target

So I have some dynamically generated HTML stuff. If you click a button, it adds some html that has another button, and you can add that other html several times, generating several copies of it. This is all inside of a

tag, so I need to use
on each button to prevent javascript from trying to submit the data. Each button also has its own code for
. There's a problem with javascript-added click events on newly generated content however. If I have
<button class="myBtn"></button>
, then in javascript I have
, that will work just fine, but only for buttons that already exist when the javascript code is run, meaning newly generated buttons won't have the event attached. The solution to that problem is found here.

Now we come to the real issue. That all works perfectly, but only until I add to
. As soon as I execute the line
document.body.innerHTML += "Text"
, the code from the link above no longer executes when the generated button is clicked.

I have an example here:

Answer Source

You are re-writing the whole body innerHTML which will remove all event listeners

Use append() instead


    document.body.innerHTML += "Now try clicking [Add new Input]"


    $('body').append("Now try clicking [Add new Input]")


Alternatively you can change your event delegation to an asset that isn't being overwritten like body or document

$(document).on("click", ".formDiv .innerContainer .innerBtn", function(e){
  $(this).before($('<input type="text" placeholder="Textbox"/>'))