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

Ming Chi Wong
Ming Chi Wong
1,073 Points

The purpose of using $(':checked').length === 0.

I don't quite understand what $(':checked').length === 0. means?

2 Answers

Steven Parker
Steven Parker
174,024 Points

The $ is the abbreviation for jQuery, so this creates a jQuery object by selecting every element that has the status of "checked" (by using the pseudo-class selector). Then it accesses the length of that collection and looks to see if it is equal to 0, meaning the list is empty.

So essentially, it's a conditional expression asking "is there nothing checked on the page?".

Steven Parker
Steven Parker
174,024 Points

No specific tag type is being selected here, but anchors don't have a "checked" attribute so they could not be selected. I believe only input tags (of type="checkbox" or "radio") can have this attribute.

If the question has been answered, you can mark it solved by choosing a "best answer".
And happy coding!

Ming Chi Wong
Ming Chi Wong
1,073 Points

Is the :checked (pseudo-class selector) part relating to the anchor tags?

Steven Parker
Steven Parker
174,024 Points

No, see the comment I added to my answer.

No It's not. The :checked selector matches all elements that are checked or selected, these include checkboxes, radio buttons and option from the select element.