Hide everything except the first element with jquery

Is there one aligned way to hide all elements of a certain type in one selector. I know you could do this:

$('p').hide(); $('p:first(or :eq(0)').show() 

Maybe something like this:

 $('p:eq(>0)') 
+6
source share
2 answers

slice() is likely to give better performance:

 $('p').slice(1).hide(); 

... where 1 is the second element in the results, and 0 is the first. This is faster because custom methods are used instead of the custom filter.

Alternatively, you can use :not() or .not() :

 $('p:not(:first)').hide(); //or $('p').not(':first').hide(); 
+18
source

http://jsfiddle.net/x6DEY/

 $("p").not(":first").hide(); 

This should work too, but ugly:

 $("div:not(:first)").hide(); 
+6
source

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


All Articles