2

So let's say I have 3 different classes: one, two, and three. Each class has 3 div's like this:

<div class="one"></div>
<div class="one"></div>
<div class="one"></div>

<div class="two"></div>
<div class="two"></div>
<div class="two"></div>

<div class="three"></div>
<div class="three"></div>
<div class="three"></div>

Then I give each class a variable:

var _1 = document.getElementsByClassName("one");
var _2 = document.getElementsByClassName("two");
var _3 = document.getElementsByClassName("three);

Then I put them all in an array call nums:

var nums = [_1,_2,_3];

If I wanted to then go through and change the text color of every single div in the classes: one, two, and three. How would I go about doing that without doing something like this:

function textColor() {
  var i;
  for (i = 0; i < _1.length; i++) {
    _1[i].style.color = "red";
  }

for (i = 0; i < _2.length; i++) {
        _2[i].style.color = "red";
      }

for (i = 0; i < _3.length; i++) {
        _3[i].style.color = "red";
      }
}

I would really like to only have one for loop that goes through and gets all the items in the array nums, and then goes through and gets every div from every item in nums and changes the text color.

4
  • 1
    Possible duplicate of How to merge two arrays in JavaScript and de-duplicate items Commented Dec 13, 2017 at 22:37
  • You can merge the arrays together, and then loop over that one array Commented Dec 13, 2017 at 22:38
  • Why don't you just add a style tag with your styles for the classes if you want to add styles dynamically. The way you are doing it atm seems unnecessary. Commented Dec 13, 2017 at 22:39
  • stackoverflow.com/questions/524696/…. Follow this to add style dynamically Commented Dec 13, 2017 at 22:41

4 Answers 4

1

Use concat when putting them in nums (and convert the NodeLists to arrays)

var nums = Array.from(_1).concat(Array.from(_2)).concat(Array.from(_3));

Or use the spread operator

var nums = [..._1,..._2,..._3];

Then you can do

function textColor() {
  nums.forEach(node => node.style.color = 'red');
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can flatten your nums array like this:

var flatNums = [].concat.apply([],nums)

and then go through it:

for (i = 0; i < flatNums.length; i++) {
    flatNums[i].style.color = "red";
}

Comments

1

I would just do something like this:
$(".one, .two, .three").prop("style","color: red;");

Or add a second class for all nine div:s.

1 Comment

Or, even shorter: $(".one, .two, .three").css("color","red"); Since jQuery is tagged, best answer. :)
0

If you will apply same style to all divs, you can simplify things even more:

divs=document.querySelectorAll('.one, .two, .three');
divs.forEach(function(el) {
el.style.color='red';
})

divs=document.querySelectorAll('.one, .two, .three');
divs.forEach(function(el) {
el.style.color='red';
})
<div class="one">1</div>
<div class="one">2</div>
<div class="one">3</div>

<div class="6">
skip
</div>

<div class="two">4</div>
<div class="two">5</div>
<div class="two">6</div>

<div class="three">7</div>
<div class="three">8</div>
<div class="three">9</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.