2

I have two classes (Column and Column2) that I want treated the differently in some ways but the same in others. I have styles set for each class individually, but for the parts I want to treat the same, I am trying to use a getElementsByClassName() to gather all of the items with either column or column2 as the class name. I cannot find a way to do that, though. I could probably just add a third class ("Columns"?) so I could select all of the elements with the "Columns" class. I was just wondering if there was an easier way other than adding a third class to a couple hundred divs.

I have tried things like:

var elements = document.getElementsByClassName("column", "column2");

All of the information I can find is for selecting an element with multiple classes, not multiple elements with different classes.

Ernesto
  • 295
  • 2
  • 13
  • 1
    For multiple classes you might want to look into using `document.querySelectorAll()` [**MDN Web Docs querySelectorAll**](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) – NewToJS Oct 06 '18 at 20:15
  • 1
    Here is a simple example of using `querySelectorAll()` for multiple classes. [**JsFiddle Demo**](https://jsfiddle.net/New_To_JS/9j5waf8n/) I believe this is what you are looking for so I hope this helps and if you have any questions please feel free to ask... – NewToJS Oct 06 '18 at 20:23
  • Thank you very much, that never showed up when I was looking for a way to select items from multiple classes, it was so easy :( – Ernesto Oct 06 '18 at 20:36
  • Possible duplicate of [getElementsByClassName() with two classes](https://stackoverflow.com/questions/37255293/getelementsbyclassname-with-two-classes) – Heretic Monkey Oct 06 '18 at 20:41
  • It would be nice if that would come up with an easy search, or if the question explained what the question was about so if I did find it in a search I would know that it would be applicable. – Ernesto Oct 06 '18 at 20:57
  • 1
    @Ernesto Well if you google *"javascript select multiple elements with different classes"* You will find many examples of using `querySelectorAll()` and at the time of posting this comment [**This is the top result**](https://stackoverflow.com/questions/13672800/select-element-with-two-classes) – NewToJS Oct 06 '18 at 21:46
  • @NewToJS - Odd, the top examples when I Google search are for selecting elements that have multiple classes. When I search on Stack Exchange, the top results are jquery questions. I have to go down eight links on Google to find one that talks about querySelectorAll() which is not far, but I generally click the Stack Exchange links, of which there are 6 of the 8, which are talking about selecting one item with two classes. – Ernesto Oct 07 '18 at 14:39

1 Answers1

1

This should do it:

var elements = document.querySelectorAll(".column, .column2");
Brainmaniac
  • 2,203
  • 4
  • 29
  • 53
  • Thank you for the assistance, I was having trouble knowing what terms to search for and that method never appeared in my searches. It took 3 seconds to implement and works perfect. – Ernesto Oct 06 '18 at 20:37