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 trialJonathan Weinstein
9,063 PointsHow to combine addEventListener and onchange?
Here's the code I'm currently having a problem with:
//Select select box
var navigationSelect = document.getElementById("nav");
//Navigate to URL when select box is changed
var navigateToValue = function() {
window.location = this.value;
}
//Send analytics data
var sendAnalytics = function() {
//Placeholder
}
navigateToValue.addEventListener("onchange", navigateToValue);
sendAnalytics.addEventListener("onchange", sendAnalytics);
//Select select box
var navigationSelect = document.getElementById("nav");
//Navigate to URL when select box is changed
var navigateToValue = function() {
window.location = this.value;
}
//Send analytics data
var sendAnalytics = function() {
//Placeholder
}
navigateToValue.addEventListener("onchange", navigateToValue);
sendAnalytics.addEventListener("onchange", sendAnalytics);
<!DOCTYPE html>
<html>
<body>
<select id="nav">
<option value="index.html">Home</option>
<option value="about.html">About</option>
</select>
<h1>Home</h1>
<script src="app.js"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<select id="nav">
<option value="index.html">Home</option>
<option value="about.html">About</option>
</select>
<h1>About</h1>
<script src="app.js"></script>
</body>
</html>
3 Answers
Colin Bell
29,679 PointsYou're trying to add an event listener to a function. You want to add the listener to the navigationSelect element, and run the respective functions when it has been changed.
navigationSelect.addEventListener('change', navigateToValue);
navigationSelect.addEventListener('change', sendAnalytics);
John Knotts
Full Stack JavaScript Techdegree Student 10,836 PointsAdam, I think you need to use "change" because it's an HTML Select and the user could potentially select a choice with only the keyboard and no mouse "click". My only hang up was that I couldn't seem to find "change" as an option in the documentation for addEventListener. :-/
Hope that helps
David Kanwisher
9,751 PointsI know I'm late, but it comes in on a Google Search so I figured I'd add another resource.
http://www.w3schools.com/jsref/event_onchange.asp http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange_addeventlistener
and Adam, I can't stop running into your old posts online haha
Adan Camacho
17,567 PointsAdan Camacho
17,567 PointsColin, if you're still around, why did you use "change" in there instead of "onchange"? I completed the challenge, but it doesn't explain where "change" came from. If it were .click, we would add "click" in the event listener, so why is this different? And what is "change"? How is "change" different from "onchange"?