1

So At the moment, I have:

<a href"..." class="site-nav__link" id="site-nav__link-id">Home</a> <a href"..." class="site-nav__link" id="site-nav__link-id">All Collections</a> <a href"..." class="site-nav__link" id="site-nav__link-id">Blog</a> <a href"..." class="site-nav__link" id="site-nav__link-id">About us</a> <!-- and more elements that are href elements. --> 
function testFunction() { var elem = document.getElementById("AccessibleNav"); var linkId = document.getElementById("site-nav__link-id"); if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) { elem.style.backgroundColor = "#333"; elem.style.width = "105%"; elem.style.textAlign = "center"; elem.style.left = "0px"; elem.style.fontColor = "white"; elem.style.position = "fixed"; linkId.style.color = "white"; } else { elem.style.backgroundColor = ""; elem.style.width = ""; elem.style.textAlign = ""; elem.style.left = ""; elem.style.color = "white"; elem.style.position = "static"; } } 

In this code I'm pretty much saying when I scroll 100px It does the top part if it's not scrolled it does the bottom part.

So when I do linkId.style.color = "white" or <br> document.getElementById("site-nav__link-id").style.color = 'white'" it only makes 1 element the first one or the one that says home into white. And I need all of them to turn white. Does anyone have a solution to this? For me to be able to either target the class and change all of the element's style or target the id and change all of the element's style.

3 Answers 3

3
var el = document.querySelectorAll('#site-nav__link-id'); for (var i = 0; i < el.length; i++) { var currentEl = el[i]; currentEl.style.color = 'white'; } 

You need to use querySelectorAll for getting multiple elements with same id.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer.
0

You have to understand the difference between classes and IDs. Classes in HTML mark elements that have something in common (similar style, similar behavior, ...). IDs however uniquely identify an element in the DOM, which means that you should only have one element with a particular ID.

The id global attribute defines a unique identifier (ID) which must be unique in the whole document.

From MDN.

This is why document.getElementById is not called document.getElementsById. For classes, you have document.getElementsByClassName, to select all elements with a specific class attribute.

By the way, this is called HTML:

<!-- You were missing a '=' before the "...". --> <!-- Don't use IDs for multiple elements! --> <a href="..." class="site-nav__link">Home</a> <a href="..." class="site-nav__link">All Collections</a> <a href="..." class="site-nav__link">Blog</a> <a href="..." class="site-nav__link">About us</a> 

... and this is called JavaScript, not Java:

var elem = document.getElementById("AccessibleNav"); 

OK, so in order to select all the elements which a specific class, use document.getElementsByClassName:

function makeThemWhite () { var elements = document.getElementsByClassName('site-nav__link'); for (var i = 0; i < elements.length; i++) { var element = elements[i]; element.style.color = 'white'; } } document.getElementById('make-them-white').addEventListener('click', makeThemWhite);
a { background-color: green; }
<a href="..." class="site-nav__link">Home</a> <a href="..." class="site-nav__link">All Collections</a> <a href="..." class="site-nav__link">Blog</a> <a href="..." class="site-nav__link">About us</a> <button id="make-them-white">Make them white!</button>

You could achieve the same using document.querySelectorAll, like @gauravmuk did:

var elements = document.querySelectorAll('.site-nav__link-id'); 

But use the class selector instead of the id selector. It works, but should not be used!

I hope I could make things clearer for you.

Comments

0

There is an easy way of doing this with current JS functionality (for of):

 var elements = document.getElementsByClassName('class_name'); for (let element of elements) { element.style.color = "cyan"; element.style.display = "initial"; } 

Just replace class_name for the class you want to select. If you just need to change one style you can compact the whole thing to just two lines:

 var elements = document.getElementsByClassName('class_name'); for (let element of elements) { element.style.display = "none"; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.