JQuery -.length return "number is not a function"

I'm new to jQuery and I was still learning, so sorry if this is a stupid question!

Basically I am trying to get the number of elements in my page with a specific class. My search tells me that I need to use .length, but it doesn't work for some reason.

Any ideas with a quick look at what might be causing the problem?

var item = $('.item')
var itemWidth = item.width();
var numberItems = item.length();
console.log(numberItems);

      

Thank!!

+3


source to share


1 answer


Simple item.length

- a property length

is a number and you are trying to call it as a function, which is why you are getting this error.



Now width

- a function; you can search .height()

. The property length

indicates how many elements match the selector (in general, the size of the jQuery collection). DOM elements are measured by width and height ( .width()

and .height()

with .innerWidth()

, .innerHeight()

, .outerWidth()

and .outerHeight()

).

+11


source







All Articles