Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Fitzhugh
9,715 PointsProblem using the "load" event type
Hi,
I'm trying to add an event listener to a <DIV> using the event type "load", but the console.log behaviour doesn't work.
However --- if I change the event type from "load" to "click", the console.log behaviour does work. So I'm pretty sure that my syntax (below) is basically correct.
const divChessboardContainer = document.querySelector("div.chessboard-container");
divChessboardContainer.addEventListener( "load", () => {
console.log("LOADED");
});
Is the "load" event-type reliable / are there problems with it? I've tried removing all of the links in my HTML file to external JS/CSS files etc but it still doesn't work.
2 Answers
Steven Parker
231,275 PointsThe "load" event can only be caught by the window, document, or elements that involve some kind of external resource (like "img'). A "div" element doesn't qualify to get a "load" event.
I can't seem to find this in MDN (my normal go-to reference), but here's a list of elements that can get a "load" event from w3schools:
<body>, <frame>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>
Samuel Ferree
31,722 Pointstry "ready" instead.
IIRC, there's some subtle different between assets being delivered to the browser ("loaded") and assets being interpreted ("ready")
Steven Parker
231,275 PointsAre you sure "ready" is a DOM event? I haven't heard of that one. Perhaps you're thinking of the jQuery method?
Samuel Ferree
31,722 PointsYeah, I think I am.
Daniel Fitzhugh
9,715 PointsDaniel Fitzhugh
9,715 PointsHmmm, yeah I was thinking it might be something like that.
I just want the event handler to run immediately, without requiring any action from the user. "Load" was mentioned by the teacher in the video, so I assumed it would be straightforward to use.
Do any alternatives spring to mind?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsAny reason you can't attach it to the body?
Daniel Fitzhugh
9,715 PointsDaniel Fitzhugh
9,715 PointsDo you mean like this ?
I tried this and it didn't do anything. But again, if I change the event type to "click" it console.logged the word "LOADED" correctly.
Steven Parker
231,275 PointsSteven Parker
231,275 PointsHow is the script loaded? Maybe the body load event has already fired before the handler is attached?
But anyway, try attaching it directly to the window instead.
Daniel Fitzhugh
9,715 PointsDaniel Fitzhugh
9,715 PointsThanks, it worked when I used "window.addEventListener" :)