0

I am using javascript to change the color of the text of a tag by mouseover function on other tag written in html. ex:

function mouseoverbox1() {
  var mypara = document.getElementById("a1");
  mypara.style.color = "green";
}
<div onmouseover="mouseoverbox1()">
  <a id="a1">Color</a>
  <a id="a1">Names</a>
  <a id="a1">Places</a>
</div>

Now as I run the code only the color of tag "Color" changes to green and not of Names and Places. Is there any way that my function could accept the changes to all anchor tags with similar id???

5
  • 9
    id need to be unique, Commented Jan 3, 2018 at 8:36
  • 3
    You cannot have multiple a tags with same IDs. You need to use classes for that. Commented Jan 3, 2018 at 8:36
  • 1
    You can't have same id for several tags in your HTML DOM. Furthermore, if you want to keep this ugly DOM, use getElementsByAttribute( "id", "a1") instead of getElementById. Commented Jan 3, 2018 at 8:37
  • Possible duplicate of JavaScript and getElementById for multiple elements with the same ID Commented Jan 3, 2018 at 8:40
  • You should use CSS for this task. Commented Jan 3, 2018 at 9:08

5 Answers 5

3

id need to be unique.The same objective can be met using document.querySelectorAll and a class, since multiple elements can have same class

//get all the elements with same class name
// iterate it using array#forEach menthod
document.querySelectorAll('.a1').forEach(function(item) {
  // add event to each of the element
  item.addEventListener('mouseover', function(e) {
    this.style.color = "green";
  })
})
<div>
  <a class="a1">Color</a>
  <a class="a1">Names</a>
  <a class="a1">Places</a>
</div>

Note: This will color individual text on mouseover, but if the requirement is to color all the text at once , then add the eventhandler to the parent element

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

Comments

3

You should use classes instead.

In Javascript you can then get an array of all elements with the same class.

function handleBoxMouseover () {
  // Get all child elements with the class "a1" and save them into an array
  var childElements = document.getElementsByClassName('a1');

  // iterate through the new array and change the color by the array index
  for (var i = 0; i < childElements.length; i++) {
    childElements[i].style.color = 'green';
  }
}
<div onmouseover="handleBoxMouseover()">
  <a class="a1">Color</a>
  <a class="a1">Names</a>
  <a class="a1">Places</a>
</div>

Just FYI:

Instead of changing the inline-styles you could also add a class to the childElements to change the color.

3 Comments

i`m just adding an answer like that :D
this will color all the text at once
@brk that's right. I wanted to keep it the same approach as the op. Your solution would work for just separated onmouseover checks.
2

Add mouseoverbox1() on each a instead of the parent div with parameter this. Then inside the function first reset the color property of all a. Finally set color only to the currently passed a.

Please Note: The id should be unique in a document. You can use class instead.

Try the following way:

function mouseoverbox1(that){
  document.querySelectorAll("#tagContainer > a.a1").forEach(function(el){
    el.style.color = "";
  });
  that.style.color = "green";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tagContainer">
    <a class="a1" onmouseover="mouseoverbox1(this)">Color</a>
    <a class="a1" onmouseover="mouseoverbox1(this)">Names</a>
    <a class="a1" onmouseover="mouseoverbox1(this)">Places</a>
</div>

2 Comments

This is a bad solutions in many ways. It fails to address the duplicate ID problem, and it would target all anchor tags on the whole page.
@connexo, I have updated my answer. Please have look. Thanks for helping me to improve my answer.
0

Is there any way that my function could accept the changes to all anchor tags with similar id???

Ids must be unique, or you will get into issues like this.

That said, you shouldn't be using getElementById if you want to keep the id same, use querySelectorAll instead

function mouseoverbox1(){
    document.querySelectorAll("[id='a1']").forEach( function( ele ){
       ele.style.color = "green";
    });
}

Demo

function mouseoverbox1(){
    document.querySelectorAll("[id='a1']").forEach( function( ele ){
       ele.style.color = "green";
    });
}
<div onmouseover="mouseoverbox1()">
    <a id="a1">Color</a>
    <a id="a1">Names</a>
    <a id="a1">Places</a>
</div>

Comments

0

Since you already have many JS solutions I will post the missing simplest (and probably most appropriate) solution for this problem: CSS.

div a:hover {
  color: green;
}
<div>
  <a id="a1">Color</a>
  <a id="a1">Names</a>
  <a id="a1">Places</a>
</div>

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.