Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery + CSS element change

Tags:

jquery

css

I'm using the following code to set all classes of the element to display:none with the exception of the first instance.

How would I modify the code to set the classes all the elements todisplay:none with the exception of the SECOND instance only.

$(document).ready(function () {
    $('selectorForElement').slice(1).css('display', 'none');
});
like image 688
Vince P Avatar asked Jan 17 '26 20:01

Vince P


2 Answers

A conjunction of :not() and :eq() selectors should do it:

$('.selectorForElement:not(:eq(1))').hide();

Live demo.

like image 190
Darin Dimitrov Avatar answered Jan 20 '26 12:01

Darin Dimitrov


$('selectorForElement:eq(0), selectorForElement:gt(1)').css('display', 'none');
like image 26
Cokegod Avatar answered Jan 20 '26 11:01

Cokegod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!