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 trial

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

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

You haven't added any "change" event handlers with the "addEventListener()" method?

Don't know what is it that I'm doing it wrong. If any one can help me with this I would appreciated.

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  
}

navigationSelect.addEventListener = ("change",navigateToValue);
navigationSelect.addEventListener = ("change",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>

2 Answers

Damien Watson
Damien Watson
27,419 Points

'addEventListener' is a method, not a variable, so you would set it this way:

navigationSelect.addEventListener("change", navigateToValue);
navigationSelect.addEventListener("change", sendAnalytics);
Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Carlos,

addEventListener is a method. Therefore you have to write the parentheses immediately after the method and then give it the parameters. You almost did it right but you divided the method and the parameters by a "=".

This should work as a solution:

//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  
}

navigationSelect.addEventListener("change", navigateToValue);
navigationSelect.addEventListener("change", sendAnalytics);