Raccoon Raccoon - 5 months ago 11
Javascript Question

Select all checkbox except the last of a specific element

I want to check if all checkbox of a specific element are checked BUT the last one
I have this kind of HTML

<ul class="progress-indicator">
<li>
//Others elements (variable)
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox"> // Exclude this one
</li>
<li>
//Stuff here
</li>
<li>
//Another stuff
</li>
</ul>


I firstly tried that

if($(".progress-indicator > li:first-child").find(":checkbox").length == $(".progress-indicator > li:first-child").find(":checkbox:checked").length)
{
//All checkbox of first li are checked
}


This is working but now I want to exclude the last checkbox of the first
li


So I started with that

$(".progress-indicator > li:first-child").find(":checkbox:not(:last-child)").length


But it did not work. This selector select only the first checkbox of the first
li
. I think it due to the
Others elements
that I have before. (The checkbox are by exemple inside a
div
which is inside another element.

So, how to select all the checkbox of the first
li
except the last checkbox OF this
li
?

Answer

Do,

$(".progress-indicator >li >input[type='checkbox']:not(:last)").prop("checked", true);