views:

854

answers:

4

Seems pretty simple but I cant get it to work.

If I have two divs with the class of 'user'

I want to output 'you have 2 divs'

    <script type="text/javascript">
  $(document).ready(function() {
     function divcount() {
      var mycount =  $('.user').length(); 
      document.write(mycount)
     }


  });
</script>

I'm sure im missing something simple..

A: 

Length is a property not a function. Size is a function.

Yuriy Faktorovich
This link worked! thanks :)
Wes
+2  A: 

It’s either $('.user').length (length property of Array) or $('.user').size() (size method of jQuery).

Gumbo
I've tried both and I can't get it to output a digit in text. How would I do this?
Wes
Remove the divcount function.
Yuriy Faktorovich
@Wes: Do you ever call that `divcount` function?
Gumbo
I've changed it to this after some googling, but I'm still not getting an output. No errors or anything. $(document).ready(function() { $('.user').size(); });
Wes
@Wes: `$(function(){ $(document.body).append( $('.user').length ); });` should do the trick.
Doug Neiner
Thanks so much for your help.
Wes
A: 

It's just $('.user').length. It's a property, not a method call.

Dave Ward
A: 
$(".user").length  // use the length property

$(".user").size()  // use the size method

notice that the code must be include in the $(function(){...}) block; like:

$(function(){
    alert( $(".user").length );
    alert( $(".user").size() );
});
www