Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery :not() selector combined with Attribute Contains selector [closed]

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.

like image 587
rlemon Avatar asked Aug 16 '11 14:08

rlemon


People also ask

What is not () in jQuery?

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.

Which CSS selectors can you not use in jQuery?

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).

What is the syntax is used to apply not equal filter on HTML elements using jQuery?

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.

What is the correct way of selecting the current element with jQuery selectors?

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.


2 Answers

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

like image 52
Mrchief Avatar answered Oct 03 '22 12:10

Mrchief


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

like image 9
David Hancock Avatar answered Oct 03 '22 12:10

David Hancock