tags:

views:

89

answers:

3

how can i count the total number of divs with same name.......i.e.

suppose i have 3 divs with same name than how can i count these three divs with jquery.

A: 

Very easy +)

$('div').size()

Or specify any additional selector parameters.

Alexander Shvetsov
+1  A: 

Like this:

var cnt = $('div[name=asfd]').length;

However, the name property is not a standard attribute for the div element.

If you are looking for a text inside the div elements:

var cnt = $('div').filter(function(){
  return $(this).text() == 'John Doe'
}).length;

You can do any comparison you like in the filter. Note that this will check all div elements in the document, so you should try to limit the number of elements in the first expression if possible.

Guffa
+1  A: 

var count = $('div').length()

Andreas Bonini
That doesn't count the number of div elements with a certain name, it counts ALL div elements.
Guffa
Yes, but what does "with a certain name" mean? Divs cannot have a name attribute.
Andreas Bonini
.length should be used for this purpose. http://docs.jquery.com/Core/size
ScottE
Thank you ScottE, edited
Andreas Bonini
@Andreas: lenght is not a function, is a property. Look at the code in my answer.
Guffa