Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - .length returning "number is not a function"

I'm new to jQuery and I'm still learning so I apologise if this is a silly question!

Basically I'm trying to get the number of elements in my page with a certain class. My searching tells me I need to use .length but it's not working for some reason.

Any ideas at a quick glance at what may be causing the issue?

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

Thanks!!

like image 208
Nick Avatar asked Mar 03 '26 17:03

Nick


1 Answers

It's just item.length - the length property is a number, and you're trying to call it as a function, so you get that error.

Now width is a function; you may be looking for .height(). The length property tells you how many elements matched the selector (more generally, the size of the jQuery collection). DOM elements are measured by width and height (.width() and .height(), along with .innerWidth(), .innerHeight(), .outerWidth(), and .outerHeight()).

like image 71
Pointy Avatar answered Mar 06 '26 05:03

Pointy