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

Naeem Mirza
Naeem Mirza
9,004 Points

Javascript Question

Please advise on what's missing from the solution below?

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

1 Answer

Andrew Cousineau
Andrew Cousineau
17,320 Points

There are a couple things going on here:

1) The last lines of your app.js have "addEventListener" misspelled. Currently they read "addEventListner"

2) Once you fix the misspelling, you will notice an issue that the select navigating from page to page will always have "Home" selected. For example, when you select "About" from the select, it will navigate to "about.html", however, the select will still read "Home", making it impossible to navigate back to "Home". To fix this, in the respective HTML files add a "selected" property to the desired option.

// about.html
<select id="nav">
  <option value="index.html">Home</option>
  <option selected value="about.html">About</option>
</select>

// index.html
<select id="nav">
  <option selected value="index.html">Home</option>
  <option value="about.html">About</option>
</select>