JQuery - How to select all EXCEPT checkboxes for a specific?

I have a jQuery selector that looks like this:

$("input:checkbox").click(function(event) { // DO STUFF HERE } 

Everything worked well until I was asked to add another flag, which has nothing to do with the original flags. How to create a selector for all flags except one? Thanks.

+6
source share
4 answers
 $('input:checkbox:not("#thatCheckboxId")').click(function(event) { // DO STUFF HERE } 

Just check the appropriate box as a unique id=""

+19
source

You can do the following:

 $("input:checkbox").not(":eq(0)") 

is the "0" index of the desired flag (in this case, the first in the DOM)

+3
source

Well ... we need to learn more about HTML for a specific answer, but there are two methods that I can think of.

1) if you know the name of the one you don't want, then delete it with not

 $("input:checkbox").not('#the_new_checkbox_id').click(function(event) { // DO STUFF HERE } 

2) use something that has the correct inputs to select them.

 $("input:checkbox[name=whatItIsNamed]").click(function(event) { // DO STUFF HERE } 
+3
source

If all the checkboxes are inside another element, you can select all the checkboxes inside this element.

Here is an example with one parent element.

 <div class="test"><input type="checkbox"><input type="checkbox"></div> 

jQuery below also works with multiple divs:

 <div class="test"><input type="checkbox"><input type="checkbox"></div> <div class="test"><input type="checkbox"><input type="checkbox"></div> 

JQuery

 $('.test input:checkbox') 

This selects only the checkboxes in any element with the class "test".

-Sunjay03

+1
source

Source: https://habr.com/ru/post/897877/


All Articles