Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Adding Multiple Event Listeners

Jonathan Weinstein
Jonathan Weinstein
9,063 Points

How 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);
app.js
//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);
index.html
<!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>
about.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
Colin Bell
29,679 Points

You'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);

Colin, 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"?

John Knotts
seal-mask
.a{fill-rule:evenodd;}techdegree
John Knotts
Full Stack JavaScript Techdegree Student 10,836 Points

Adam, 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
David Kanwisher
9,701 Points

I 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