Noam B.
Noam B.

Reputation: 3250

select all checkboxes inside ul

I would like to understand.

Why this will not work:

$(this).closest("li").find("ul:checkbox").prop("checked", true);

But this will?

$(this).closest("li").find("ul").find(":checkbox").prop("checked", true);

Thanks

Upvotes: 2

Views: 1705

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337656

The issue in your first example is because of ul:checkbox. This looks for <ul> elements which are also checkboxes, which is not possible. You need a space between those selectors, or to use find() as you are in the second example.

Either of these will work:

$(this).closest("li").find("ul :checkbox").prop("checked", true);
// or
$(this).closest("li").find("ul").find(":checkbox").prop("checked", true);

Upvotes: 7

Related Questions