1

I need to loop through an array of 3 class elements and change the background of the second one that has the class using pure Javascript.

Here's code that I'm using but it's applying red to all 3 blocks regardless instead of only the second:

var x = document.getElementsByClassName("newClass");
var i;
for (i = 0; i < x.length; i++) {
  if (x[1]) {
    x[i].style.backgroundColor = "red";
  } else {
    x[i].style.backgroundColor = "yellow";
  }
}

Is there an easier way of doing this with pure Javascript.

I'd appreciate any help with this.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
new_coder
  • 211
  • 1
  • 7
  • Possible duplicate of [For-each over an array in JavaScript?](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – Jodast Mar 13 '19 at 02:22

6 Answers6

1

I think this is the solution to the problem

  var x = document.getElementsByClassName("newClass");
  var i;
  for (i = 0; i < x.length; i++) {
      x[i].style.backgroundColor = "yellow";
      if (i ==1) {
          x[i].style.backgroundColor = "red";
      }}
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>

and I also think this is a problem solving

var x = document.getElementsByClassName("newClass");
var i;
for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "yellow";
    }
document.getElementsByClassName("newClass")[1].
   style.backgroundColor = "red"
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
1

If you prefer ES6:

const elements = document.querySelectorAll('newClass');
if (elements) {
  // color them all yellow
  elements.map(element => element.style.backgroundColor = 'yellow');
  // color the second element red
  if (elements[1]) elements[1].style.backgroundColor = 'red';
}
BugsArePeopleToo
  • 2,976
  • 1
  • 15
  • 16
0

You need to check whether i == 1, not x[1]:

var x = document.getElementsByClassName("newClass");
var i;
for (i = 0; i < x.length; i++) {
    if (i == 1) {
        x[i].style.backgroundColor = "red";
    } else { 
        x[i].style.backgroundColor = "yellow";
    }
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

I'm not sure if this is exactly what you need but in the question you just asked about changing the background color of the second element, so this should be enough. Let me know if this isn't what you're looking for.

document.getElementsByClassName("newClass")[1].style.backgroundColor = "red";
Chris Sandvik
  • 1,787
  • 9
  • 19
0

If you want to loop over the elements with that class you can do this

let x = document.getElementsByClassName("newClass");
[...x].forEach((item, i) => {
  if (i == 1) {
    item.style.background = "red";
  }
});
Joe Koker
  • 931
  • 10
  • 20
0

// typeof x is object becareful

let x = document.getElementsByClassName("newClass");
[...x].forEach((item, i) => {
    item.style.background = "yellow";
});
[...x][1].style.background="red";
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
<h1 class="newClass">hello world</h1>
Nemer
  • 667
  • 1
  • 6
  • 10