addEventListener gone after appending innerHTML

Posted by Vishal Shah on Stack Overflow See other posts from Stack Overflow or by Vishal Shah
Published on 2010-04-21T17:01:09Z Indexed on 2010/04/21 17:03 UTC
Read the original article Hit count: 283

Okay, so i have the following html added to a site using javascript/greasemonkey. (just sample)

  • *a id='abc'*HEllo*/a*
  • *a id='xyz'*Hello*/a*

(excuse me, i've had to replace the '<' '>' with * since hyperlinks for new users aren't allowed!)

and i've also added a click event listener for the elements. All works fine up to this point, the click event gets fired when i click the element.

But... i have another function in the script, which upon a certain condition, modifies that html, ie it appends it, so it looks like:

  • *a id='abc'*HEllo*/a*
  • *a id='xyz'*Hello*/a*
  • *a id='123'*Hello*/a*

but when this is done, it breaks the listeners i added for the first two elements... nothing happens when i click them.

if i comment out the call to the function which does the appending, it all starts working again!

help please...

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about addeventlistener