JavaScript Javascript appending class over existing class javascript appending class,javascript adding class,javascript adding class to

I am having a problem implementing the Query Selector All method with classList. Basically, what I want to do is add the class .purple to all instances of the fridge class. However, it keeps telling me that it cannot add to undefined.

The code snippet below hopefully illustrates what I am trying to accomplish.

document.querySelectorAll('.fridge').classList.add('purple');
.purple {color: purple;}
<div class="fridge">Stuff</div>
<div class="fridge">Moar Stuff</div>
Answer:1

You need to convert the NodeList from querySelectorAll into an array and loop over it:

Array.prototype.slice.call(document.querySelectorAll('.fridge')).forEach(function (fridge) {
  fridge.classList.add('purple');
});
.purple {
  color: purple;
}
<div class="fridge">Stuff</div>
<div class="fridge">Moar Stuff</div>
Answer:2

Here is very easy-to-read code

var items = document.querySelectorAll('.fridge');
for (var x = items.length - 1; x >= 0; x--){
    items[x].classList.add('purple');
}
.purple {color: purple;}
<div class="fridge">Stuff</div>
<div class="fridge">Moar Stuff</div>
Answer:3

I'm using the AngularSlideables to smoothly expand and collapse an area where people can find more details with the following code (Fiddle here): <h3 slide-toggle="#details" >More details</...

I'm using the AngularSlideables to smoothly expand and collapse an area where people can find more details with the following code (Fiddle here): <h3 slide-toggle="#details" >More details</...

I have a directive that should open file picker window when a controller variable changes. Here is the snippet of the directive: angular.module('settingsInternal') .directive('triggerUpload', ...

I have a directive that should open file picker window when a controller variable changes. Here is the snippet of the directive: angular.module('settingsInternal') .directive('triggerUpload', ...

i'm trying to make captions in owl carousel. i'm using animate.css. I've added animation to captions in carousel but it's not working for all. Only first slides caption have animation. Here is my code;...

i'm trying to make captions in owl carousel. i'm using animate.css. I've added animation to captions in carousel but it's not working for all. Only first slides caption have animation. Here is my code;...

  1. carousel with caption
  2. carousel with caption bootstrap 4
  3. carousel with caption codepen
  4. carousel with caption bootstrap
  5. carousel with caption jquery
  6. carousel with caption example
  7. carousel caption on top
  8. carousel caption in the middle
  9. carousel caption in center
  10. carousel caption on right
  11. carousel caption in html
  12. owl carousel with caption
  13. owl carousel with caption example
  14. amp carousel with caption
  15. carousel slider with caption
  16. bootstrap carousel with caption animation
  17. owl carousel with caption example codepen
  18. slick carousel with caption
  19. image carousel with caption
  20. waterwheel carousel with caption'

I'm trying to make a list toggle code effecient. I plan on making about 20 lists and copying pasting the code and changing the classes seems really inefficient. I just can't figure out a way to make ...

I'm trying to make a list toggle code effecient. I plan on making about 20 lists and copying pasting the code and changing the classes seems really inefficient. I just can't figure out a way to make ...

  1. jquery toggle list items
  2. jquery toggle list
  3. jquery toggle list example
  4. jquery toggle list view
  5. jquery toggle list elements
  6. jquery classlist toggle
  7. jquery toggle ul list
  8. jquery toggle effects list
  9. jquery toggle checkbox list
  10. jquery toggle display list item
  11. jquery toggle faq list
  12. jquery toggle ul list javascript