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
Kieran Barker
15,028 PointsHow does one loop through elements in pure JavaScript?
I work for a small web development firm. I joined as an apprentice without a solid foundation in JavaScript. I am working through the courses here at Treehouse to improve my knowledge. That said, we use jQuery at work so I am now comfortable looping through elements with the .each() function like so:
$("nav a").each(function() {
// jQuery here
});
I am currently working on a personal project to improve my knowledge of pure JavaScript loops. How would I accomplish the same thing as above? Here's my current code:
HTML:
<nav>
<ul>
<li><a href="#while">While Loops</a></li>
<li><a href="#do-while">Do While Loops</a></li>
<li><a href="#for">For Loops</a></li>
<li><a href="#for-in">For In Loops</a></li>
</ul>
</nav>
CSS:
nav ul {
list-style: none;
padding: 0;
margin: 0;
}
nav a {
display: block;
padding: 15px;
transition: color 0.3s;
}
nav a.selected,
nav a:hover {
color: #000;
transition: color 0.3s;
}
I want the currently selected navigation item to be black, as per the .selected rule. But because each of the navigation items links to a heading with an ID, and not a separate page (whereby I would have had separate HTML files and added the .selected class manually), I don't think the class can be added to them unless I add an event listener in JavaScript.
So basically, how can I loop through each of my navigation items, add an event listener for click, and then onclick, remove the .selected class from whichever element currently has it and add it to the one that's been clicked?
I hope this makes sense!
1 Answer
William Warme
43 Points//get all anchor elements
var navElements = document.querySelectorAll('a');
//looping through each anchor element
navElements.forEach(function(element){
//adding click event on each anchor element
element.addEventListener('click',function(e){
//stop default behaviour
e.preventDefault();
//select current active element
let active = document.querySelector('.selected');
active.classList.remove('selected'); //remove class
this.classList.add('selected'); //add class to current click element
});
});
Kieran Barker
15,028 PointsKieran Barker
15,028 PointsHi William,
Thanks for your response. Does the
.preventDefault()part mean that the links will no longer work?Thanks,
Kieran :)
Austin Whipple
29,847 PointsAustin Whipple
29,847 PointsKieran,
Essentially, yes. William passing
ein and usinge.preventDefault();"breaks" any default behavior the browser would initiate on a click. This is used frequently on click events, especially when replacing the behavior of an anchor tag. You can read more on the MDN.