Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript get all elements by class name, while excluding specific classes? [duplicate]

So let's say I have the following website:

<div class="hello"></div>
<div class="hello"></div>
<div class="hello world"></div>
<div class="hello"></div>

Using the following code, I save all elements that have the class "hello" into an array:

var inputs = document.getElementsByClassName('hello');

Is there any way I can exclude all elements that have the class "world" so I can only get three results?

like image 489
Noniq Avatar asked Aug 05 '16 11:08

Noniq


People also ask

How do you get all elements in a particular class?

To do this we will use HTML DOM querySelectorAll()method. This method of HTML DOM (document object model) returns all elements in the document that matches a specified CSS selector(s). The syntax to use this method is as follows. Example: The CSS selectors would be class, ids, or any HTML tags.

How can I get all of the elements with the same class name in JavaScript?

Get elements by class namegetElementsByClassName('class-name') , it grabs all the elements that have the same class name and returns us an HTML Collection that we can index or iterate to get the elements that we need in particular.

What does getElementsByClassName () do in JavaScript?

getElementsByClassName() The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.

What does getElementsByClassName () function return?

The getElementsByClassName() method returns a collection of elements with a specified class name(s). The getElementsByClassName() method returns an HTMLCollection.


1 Answers

Theoretically you can use document.querySelectorAll()

// searches for all <div> elements with the class of 'hello',
// and without the class of 'world':
var inputs = document.querySelectorAll('div.hello:not(.world)');

var inputs = document.querySelectorAll('div.hello:not(.world)');
console.log(inputs);
<div class="hello"></div>
<div class="hello"></div>
<div class="hello world"></div>
<div class="hello"></div>

Or you can simply convert the NodeList into an array, and filter that array:

// searches for all elements with the class-name of 'hello':
var inputs = document.getElementsByClassName('hello'),

    // Array.prototype.slice.call(inputs,0) uses the Array
    // method of slice on the NodeList returned by
    // document.getElementsByClass(), turning it into an
    // Array:
    inputsArray = Array.prototype.slice.call(inputs, 0)
                    // then we filter the Array:
                    .filter(function (el) {
                      // el: a reference to the current
                      //     Array element of the Array
                      //     over which we're iterating.

      // el.classList.contains returns a Boolean
      // true if the 'el' contains a class of
      // 'world', and false if not; we invert that
      // using the ! (not) operator because
      // Array.prototype.filter() retains elements
      // should the evaluation be true/truthy;
      // whereas we want to keep the elements for
      // which classList.contains() is false:
      return !(el.classList.contains('world'));
    });

var inputs = document.getElementsByClassName('hello'),
  inputsArray = Array.prototype.slice.call(inputs, 0).filter(function(el) {
    return !(el.classList.contains('world'));
  });

console.log(inputsArray);
<div class="hello"></div>
<div class="hello"></div>
<div class="hello world"></div>
<div class="hello"></div>
like image 191
David Thomas Avatar answered Oct 01 '22 10:10

David Thomas