JavaScript Equivalent of getElementById for Classes in Javascript

I'm currently making a game, and I'm trying to change the CSS values of a whole class of objects. For a single ID, I would use, document.getElementById("idHere"), but I need something like document.getElementByClass("classHere"). Thanks!

Answer:1

There is simply document.getElementsByClassName("myClass"), which returns an array of all the elements with that HTML class.

If you're using jQuery, you can do it with $(".myClass"), which will return a collection of all of the elements with that class.

Answer:2

There exists getElementsByClassName; see document.getElementsByClassName on MDN.

Answer:3

If you can use JQuery I suggest $('.classHere').

Answer:4

Two ways to do it:

document.getElementsByClassName("theClassName");
document.querySelectorAll(".theClassName");

These methods won't work on older browsers (like IE7 or IE8). In this case you'll have to iterate through elements to check the class name, or rely on a library like jQuery.

Answer:5

If you can't or don't want to use jQuery, do the following:


function changeClassName(className, newClassName) {

    var elements = document.getElementsByClassName(className);

    for (var i = 0; i < elements; ++i) {
        var item = elements[i];  
        item.className = newClassName;
    }

}


If you decide to use jQuery:

function changeClassName(className, newClassName) {

    $('.'+className).toggleClass(newClassName);

}

Answer:6

I have the following code jQuery $(".col-md-12").hide(); $(".button-div").hide(); $(".featurette-divider").hide(); $(".footer").hide(); $(".first").fadeIn(1000); $(".second").delay(900).fadeIn(1000); ...

I have the following code jQuery $(".col-md-12").hide(); $(".button-div").hide(); $(".featurette-divider").hide(); $(".footer").hide(); $(".first").fadeIn(1000); $(".second").delay(900).fadeIn(1000); ...

I am trying to add some obstacles to the canvas game that I've got but something seems to be wrong and I can't seem to put my finger on it. I just want some simple walls here and there to make the ...

I am trying to add some obstacles to the canvas game that I've got but something seems to be wrong and I can't seem to put my finger on it. I just want some simple walls here and there to make the ...

I am trying to use this example as shown HERE in JSFIDDLE, the problem i am having is that I would like when dropping to squeeze the new record in between and not swap. I tried adding a for loop in ...

I am trying to use this example as shown HERE in JSFIDDLE, the problem i am having is that I would like when dropping to squeeze the new record in between and not swap. I tried adding a for loop in ...

I have created the javascript based demonstration and now I want to hide an image on click in certain condition only. $("#clear").click(function(){ if ($('#image').is(":...

I have created the javascript based demonstration and now I want to hide an image on click in certain condition only. $("#clear").click(function(){ if ($('#image').is(":...

  1. multiple conditions javascript if
  2. multiple conditions javascript
  3. filter multiple conditions javascript
  4. case multiple conditions javascript
  5. ternary multiple conditions javascript
  6. switch multiple conditions javascript
  7. check multiple conditions javascript
  8. multiple conditions in javascript if statement
  9. while multiple conditions javascript
  10. multiple conditions for javascript .includes() method
  11. multiple conditions in javascript for loop
  12. validate multiple conditions javascript
  13. set multiple conditions javascript
  14. add multiple conditions javascript
  15. ternary operator multiple conditions javascript
  16. switch case multiple conditions javascript
  17. if else multiple conditions javascript
  18. array filter multiple conditions javascript
  19. while loop multiple conditions javascript
  20. do while multiple conditions javascript