jQuery: How to select all elements which have an attribute that is not equal to specific value?

How could I select in jQuery all elements that have the my_attr attribute which is not equal to my_value ?

If my_attr is a and my_value is "4", it should work like this:

<span>Hello</span>          => Not selected
<span a="5">Stack</span>    => Selected
<span b="4">Overflow</span> => Not selected
<span a="4">!!</span>       => Not selected

Answers


To get a "doesn't match", you'd use an attribute not-equals selector with (the other part of the question) as has-attribute selector, like this:

$("span[a][a!='4']")

If you want it to equal, just take out the ! for an attribute-equals selector, like this:

$("span[a][a='5']")

To use a variable, just concatenate, like this:

$("span[" + my_attr + "][" + my_attr + "!='" + my_value + "']")

Need Your Help

Why am I seeing an ARC issue saying I did declare a method with a particular selector?

objective-c ios cocoa-touch automatic-ref-counting

Guess it's a simple question, but after googling and reading in a lot of documents, i'm still stuck with a stupid error. So I guess, i have some understnding problems according to ARC ...

line breaks using javascript strings

javascript string line-breaks

I m breaking a line in javascript using \n for example: -

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.