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

Jarrett Young
Jarrett Young
12,635 Points

stuck on this code challenge, kinda lost

I'm stuck on this code challenge because I'm not understanding what's being asked of me. Anyone out there who can clarify?

Challenge: We've got a select box that's used to navigate to a new page on the "change" event and record some analytics. We want two event handlers to fire, fix lines 14 and 15 so that both of the handlers trigger on "change" rather than just the last function assigned to onchange.

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.onchange = navigateToValue.addEventListener();
navigationSelect.onchange = sendAnalytics.addEventListener();
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>
Jarrett Young
Jarrett Young
12,635 Points

never mind, finally figured it out!!

1 Answer

how did you figure it out??? m still lost

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

For anyone who may be stuck on this challenge, first we need to add two event listeners that each carry two parameters, one signifying that there is going to be a change when the event handler(s) is called and another that adds the task(s) that needs to be undertaken:

//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);