How can I count the number of elements with same class?

JavascriptJquery

Javascript Problem Overview


I have a main div in my page with a specific id. Now some input elements of the same class are present in this div. So how can I count the number of these elements of same class in this div using jQuery?

Javascript Solutions


Solution 1 - Javascript

With jQuery you can use

$('#main-div .specific-class').length

otherwise in VanillaJS (from IE8 included) you may use

document.querySelectorAll('#main-div .specific-class').length;

Solution 2 - Javascript

document.getElementsByClassName("classstringhere").length

The document.getElementsByClassName("classstringhere") method returns an array of all the elements with that class name, so .length gives you the amount of them.

Solution 3 - Javascript

You can get to the parent node and then query all the nodes with the class that is being searched. then we get the size

var parent = document.getElementById("parentId");
var nodesSameClass = parent.getElementsByClassName("test");
console.log(nodesSameClass.length);

<div id="parentId">
  <p class="prueba">hello word1</p>
  <p class="test">hello word2</p>
  <p class="test">hello word3</p>
  <p class="test">hello word4</p>
</div>

Solution 4 - Javascript

$('#maindivid').find('input .inputclass').length

Solution 5 - Javascript

I'd like to write explicitly two methods which allow accomplishing this in pure JavaScript:

document.getElementsByClassName('realClasssName').length

Note 1: Argument of this method needs a string with the real class name, without the dot at the begin of this string.

document.querySelectorAll('.realClasssName').length

Note 2: Argument of this method needs a string with the real class name but with the dot at the begin of this string.

Note 3: This method works also with any other CSS selectors, not only with class selector. So it's more universal.


I also write one method, but using two name conventions to solve this problem using jQuery:

jQuery('.realClasssName').length

or

$('.realClasssName').length

Note 4: Here we also have to remember about the dot, before the class name, and we can also use other CSS selectors.

Solution 6 - Javascript

Simplest example:

document.getElementById("demo").innerHTML = "count: " + document.querySelectorAll('.test').length;

<html>
    <body>
    
    <p id="demo"></p>
    <ul>
      <li class="test">Coffee</li>
      <li class="test">Milk</li>
      <li class="test">Soda</li>
    </ul>

 </body> 
 </html>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmanishjangirView Question on Stackoverflow
Solution 1 - JavascriptFabrizio CalderanView Answer on Stackoverflow
Solution 2 - JavascriptSheshank S.View Answer on Stackoverflow
Solution 3 - Javascriptuser7396942View Answer on Stackoverflow
Solution 4 - JavascriptsandeepView Answer on Stackoverflow
Solution 5 - JavascriptsimhumilecoView Answer on Stackoverflow
Solution 6 - JavascriptLunar SutarView Answer on Stackoverflow