3

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!!

1 Answer 1

11

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()).

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant! This was driving me insane. Baby steps I guess! Thank you for your help! :)
This solved my problem. To clarify, in Java it's string.length(), in JavaScript it's string.length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.