ale
ale

Reputation: 11820

jquery selector - selecting all span tags with id not equal to something

I want to select all HTML <span> elements that don't have an id equal to x and hide them them. Is this possible? I think you have to use the :not selector but I can't work it out:

$('span:not(#x').attr('style', "display: none;");

Any ideas? Thank you :).

Upvotes: 4

Views: 16746

Answers (4)

Alessandro Vendruscolo
Alessandro Vendruscolo

Reputation: 14875

There are at least three ways to do what you need.

$('span:not(#x)').attr('style', "display: none;");
$('span[id!="x"]').attr('style', "display: none;");
$('span').filter(':not(#x)').attr('style', "display: none;");

The most efficient way is $('span[id!="x"]').attr('style', "display: none;");

See http://jsfiddle.net/xpAeK/

Upvotes: 3

dknaack
dknaack

Reputation: 60486

$('span[ID!="x"]').attr('style', "display: none;");

sets the style attribute of all spans, wich has NOT the id x, to none.

hope this helps

Upvotes: 4

Filip
Filip

Reputation: 2522

You just forgot a ).

I made a fiddle to show you. http://jsfiddle.net/3U8tD/

$('span:not(#x)').attr('style', "display: none;");

Upvotes: 13

Toni Toni Chopper
Toni Toni Chopper

Reputation: 1851

try this

$('span[id!="x"]').attr('style', "display: none;");

Upvotes: 1

Related Questions