Hide all divs (except the first) having the same class

I have 10 <div> with the same class

<div class="c1"></div> <div class="c1"></div> <div class="c1"></div> <div class="c1"></div> <div class="c1"></div> <div class="c1"></div> <div class="c1"></div> <div class="c1"></div> <div class="c1"></div> <div class="c1"></div> 

Now I need to hide all the <div> leaving it first.

+4
source share
4 answers

This is the fastest way to do this: $('div.c1').not(':eq(0)').hide(); :)

+16
source
 $('.c1').hide(); $('.c1:first').show(); 
+1
source

Another way:

 $('div.c1:gt(0)').hide (); 

Which is more flexible if you decide to keep 1st 2, for example.

Note that this is also 10% faster than other answers so far (in FF 5). See this performance test .

0
source
 var elements = document.getElementsByTagName("div").getElementsByClassName("c1"); for (var i = 1; i < elements.length; i++) { elements[i].style.visibility = "hidden"; } 

I hope it works

0
source

All Articles