Tuesday, 15 June 2010

javascript - Creating an element that can remove it self? -


I am creating a lightbox as a school project, and I can not use jQuery. I have an image when you click it, creates a transparent div with the javascript id "overlay" I want to remove the div myself, or parents to remove it, but it does not work I think what to do with the fact that you can not link 'onclick' to an element that is not yet present.

You have to remove the element from your parents. Something like this:

  d = document.getElementById ('overlay'); D.parentNode.removeChild (d);   

Or you can hide it:

  d.style.display = 'none';   

And, oh: You can add javascript code to the element (newly created) by specifying a function for the onclick attribute.

  d = document createElement ('div'); D.onclick = function (e) {this.parentNode.removeChild (this)};    

No comments:

Post a Comment