Search code examples
javascriptjqueryjquery-selectors

How do I use the :not() selector for many values in js


I am coding on js and I was trying to get all the checkboxes that have the class 'some-class', the name 'someName[]' and NOT the ones with value of 'typeOf : category'. Something like this:

<input class="some-class" type="checkbox" value="{"typeOf": "category", "ids":["12345678"]}" name="someName[]">

I was trying something like this:

 checkboxes: $('.some-class[name="someName"]:not([value="typeOf : category"])'),

This didn't work. I guess you guys can help me. Thank you all :)


Solution

  • Using a filter() function might give you more flexibility than creating an ugly selector

    $('.some-class[name="someName[]"]').filter(function(){
       return !this.value.startsWith('{"typeOf": "category"') 
    }).prop('checked', true)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Shouldn't match: <input class="some-class" type="checkbox" value='{"typeOf": "category", "ids":["12345678"]}' name="someName[]">
    
    Should match: <input class="some-class" type="checkbox" value='foo' name="someName[]">