Hello. I'm Aleksandar Vacić, professional web developer and wine maker in the making.
Learn more about me or see what I can do for you.

My work & services

innerHTML kills event handlers

This is probably old news to regular Javascript coders, but for me it was an ugly bite. I have renewed my interest in JS with the abundance of libraries springing up and started coding few solid client side interfaces in the office.

A certain interface was previously built in a hurry and HTML code was littered with plenty onclick="somefunction()" code. I re-factored all of that so that handlers were added on DOM.load; however, there was one nasty feature left in the code.

I had a table of 6 rows and button which that says “Add line”. Adding lines is easy: create the whole table row HTML on server, fetch that with XHR call and do tbody.innerHTML += sRow. Actually, this was the code I used for IE and as soon as this is done, all the previously set event handlers went missing. Like they never existed.

Bottom line: changing innerHTML property removes all late-bound event handlers and script on that piece of the page.

Solution was to get off my lazy fingers and type in proper DOM calls: insertRow and insertCell. Clean code and no browser branching. :)

tags: Web dev
Comments: 0

Posted 3 years, 5 months agopermanent link

trackback URL: http://aplus.rs/web-dev/innerhtml-kills-event-handlers/trackback/

Voices from the crowd

No comments posted yet. Your chance to be the first.

Post your opinion


? You need to enter your email, but rest assured it will not be published.

? Enter the link and it will be published.
? This blog strives to behave properly, as per XHTML 1.0 Strict spec. You can use the following tags, but please use them wisely:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
? I have enabled comment moderation so if your comment does not shows up after you submit it, don't worry. I will review it and if it's nice and to the point, I'll approve it. This measure is up mostly to fight spam and trolls.

Tags or categories or topics...

Lots of ramblings on this blog...might be easier for you to find your juice through these tags:

Post a job. Find one. authenticjobs.com