Jquery selector - select all span tags with id not equal to anything

I want to select all HTML <span> elements that have no id equal to x and hide them. Is it possible? I think you need to use the :not selector, but I cannot solve it:

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

Any ideas? Thanks:).

+4
source share
4 answers

You just forgot a).

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

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

sets the style attribute of all spans that does NOT have id x, by anyone.

hope this helps

+4
source

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/

+3
source

try it

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

Source: https://habr.com/ru/post/1384111/


All Articles