So I have a large form and I need to select all elements that have a specific identifier in their id value.
$("[id*=some-value]")
This works wonderfully! Now I need to filter out of these results any elements that have another key identifier in their id values
$("[id*=some-value]:not([id*=some-other-value])")
which obviously is not working for me.
Currently the only element I am filtering is a checkbox so I can just use
$("[id*=add-contact-form]:not(:checkbox)")
however I would still like to know how to combine the two selector methods.
The not() is an inbuilt function in jQuery which is just opposite to the filter() method. This function will return all the element which is not matched with the selected element with the particular “id” or “class”. Syntax: $(selector).not(A) The selector is the selected element which is not to be selected.
jQuery :not() Selector The :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).
jQuery not() Method The not() method returns elements that do not match a certain criteria. This method lets you specify a criteria. Elements that do not match the criteria are returned from the selection, and those that match will be removed.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
First of all, if your id contains "some-value" literally, then it'll automatically exclude "some-other-value".
For it to be able to pick up the other elements, the id
has to match upto a point: "some-other-value" -> "some-value-other" (see how the first 2 portions match)
You can try this:
$("[id*=add-contact-form]").not(":checkbox");
or
$("[id*=some-value]").not("[id*=some-value-other]");
DEMO
What you already have seems to be working fine for me?
I suggest taking a look at your code and seeing if there is some underlying issue preventing that jQuery selector from working.
Take a look
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With