chrisstump
3/14/18 at 2:25 pm
JavaScript
jquery
Counting Elements with jQuery
Checking the length of a given element will return the number of instances of that particular element. This is particularly useful for checking to see if an element exists.
1
2
3
var myElementCount = $('.my-element').length; // myElementCount = 3 // you can also use this to check if an element exists if(myElementCount > 0) { // yes, at least one matching element exists } else { // no matching elements }
Copy Snippet to Clipboard
Don't Have an Account?
Stop digging through old code examples. Get organized. Get Code Snipper.
Learn More
Start Free 14 Day Trial