I have this very simple demo:
function foo() { alert('Works!'); } var inp = document.createElement('input'); inp.onblur = foo; document.body.appendChild(inp);
See here: http://jsfiddle.net/A7aPA/
As you can see, this works. (Click on the input, then click somewhere else and an alert will pop up.)
However, if I add this line to the JavaScript code:
document.body.innerHTML += '<br>';
then the blur handler stops working (and no error is thrown btw).
See here: http://jsfiddle.net/A7aPA/1/
Why is that?
'innerHTML' Removes Event Listeners Since the entire tag (the div in our code) is being reparsed, the event listener is lost in the process. Rather than using the += operator, use DOM functions such as append() .
The innerHTML property is part of the Document Object Model (DOM) that allows Javascript code to manipulate a website being displayed. Specifically, it allows reading and replacing everything within a given DOM element (HTML tag).
People can struggle and complain about innerHTML not working. Such things usually occur because of human error, when strings are not appropriately defined, or there are some mistakes in JavaScript code.
Yes, when you do:
document.body.innerHTML += '<br>';
You're really doing:
document.body.innerHTML = (document.body.innerHTML + '<br>');
So you're completely destroying and recreating all the content.
Modifying innerHTML
causes the content to be re-parsed and DOM nodes to be recreated, losing the handlers you have attached. Appending elements as in the first example doesn't cause that behavior, so no re-parsing has to occur, since you are modify the DOM tree explicitly.
Another good way to handle this is to use insertAdjacentHTML(). For example:
document.body.insertAdjacentHTML('beforeend', '<br>')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With