JavaScript jQuery Basics Working with jQuery Collections Stopping the Browser's Default Behavior

Amandeep Pasricha
Amandeep Pasricha
14,928 Points

Why did we write .length?

Could someone explain this conceptually? What is the object that we are applying the length property to?

1 Answer

Christopher Gardner
Christopher Gardner
12,347 Points

Hi Amandeep, it looks like it's running through the pdfs in the list, and using jQuery's :checked selector, you're searching to see if any of the boxes are checked. So it's saying if the length of the number of boxes checked is 0, then prevent downloads from happening. If the checkbox is selected, the length will return 1, allowing for the pdfs to be downloaded.

Hope this helps!

Amandeep Pasricha
Amandeep Pasricha
14,928 Points

Hey but what I don't get is :checked some sort of object? Because you only write .length or .something when an object is involved. So what's the object in this case? Does jquery always return some sort of object? I guess this is more of a fundamental CS conceptual type of question.

Christopher Gardner
Christopher Gardner
12,347 Points

Amandeep Pasricha, you're essentially selecting all of the checkboxes that are checked, which you can then use length on. For example, think about it as if you were using document.querySelectorAll('li'). You've selected all the list items, and can then apply .length on them. It's the same for :checked. You've just selected all the checked checkboxes.