Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: See how many elements a selector matched?

Tags:

If I have a selector like

$.('.active'); 

How can I see how many items that matched?

Alternatively, is there an easy way to see if more than zero elements were matched?

like image 255
Nick Heiner Avatar asked May 15 '10 01:05

Nick Heiner


People also ask

How do I count the number of elements in jQuery?

Answer: Use the jQuery . length property You can simply use the jQuery . length property to find the number of elements in a DIV element or any other element.

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

Which jQuery selector is fastest?

ID and Element selector are the fastest selectors in jQuery.

How many jQuery selectors are there?

So far we have covered only three standard jQuery Selectors. For a complete detail of all these jQuery selectors, you can go to through jQuery Selectors Reference.


1 Answers

call .length on the returned set.

Do not use .size because:

The .size() method is deprecated as of jQuery 1.8

like image 104
x1a4 Avatar answered Feb 17 '23 22:02

x1a4