Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript windows.onload or referencing new elements?
    text
    copied!<p>I'm trying to create a lightbox and I'm having trouble. </p> <p>I think the problem is either because I have 2 window.onloads or because I'm trying to reference a newly created DOM element. I added some comments in the code below that explain what I'm trying to do. </p> <pre><code>//open lightbox window.onload = showLargeImage; function showLargeImage() { var enlargeButton = document.getElementById("thumb1"); // thumb1 is a thumbnail you click to get the lightbox enlargeButton.onclick = handleClick; } function handleClick() { var lightboxContainerId = document.getElementById("lightboxContainer"); lightboxContainerId.innerHTML = '&lt;div class="lightbox"&gt;&lt;a class="reduceButton" href="#" &gt;&lt;img id="imageButton" class="largeImage" src="2012/images/web/web1.jpg" width="500" height="500" alt="Web Thumb 1"&gt;&lt;/a&gt;&lt;/div&gt;'; } // the inner HTML creates the lightbox. //close lightbox window.onload = reduceImage; // i'm pretty sure that this windo.onload is the problem... or, that I'm referencing "imageButton" which is new to the DOM function reduceImage() { var reduceButton = document.getElementById("imageButton"); // you're supposed to click the big image in the lightbox to get close it. reduceButton.onclick = handleReduceClick; } function handleReduceClick() { var shadeId = document.getElementById("lightboxContainer"); shadeId.innerHTML = "say what"; // closing the lightbox simply strips everything out of the lightboxContainer alert("oh yeah"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload