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 trialRabe Datta
3,693 Pointsdidn't work with addEventListener
const clickButton = document.getElementById("load");
clickButton.addEventListener("click", () => {
xhr.send();
clickButton.style.display = "none";
})
the above code didn't work with AJAX. Not sure why.
3 Answers
Robert Manolis
Treehouse Guest TeacherHi Rabe, I can't say for sure without seeing all the code you're working with, but I just tested this out in the Workspace for that particular video. If you're doing what's demoed in the video, the problem is that the code bewteen the script tags is getting parsed before the HTML. So there is no DOM element with the id of "load" at the time the JS is parsed. It works in the sendAJAX function, because the code in that function doesn't run until the click occurs. And by that time, the DOM element with the id of "load" definitely exists.
If you break all the JS out into its own JS file, what you have should work fine. Or at least if worked for me when I tested it.
Hope that helps.
Rabe Datta
3,693 PointsThank you, Robert!
stephen khalaf
6,738 Pointsadd a script tag before the end of the <body> tags, and insert your code. The addEventListener will work there because the DOM would have been loaded