Vince P
Vince P

Reputation: 1791

Change CSS value when page has loaded on all EXCEPT first instance

I have the following piece of code do change the value of a division once the page has loaded but I was wondering if it would be possible to amend it to change the value of all of the divisions with that class with the exception of the first one.

jQuery('#accordion div.pane').css('display', 'none');

Upvotes: 1

Views: 203

Answers (3)

karim79
karim79

Reputation: 342635

Simply use the :gt selector to select all but the first:

jQuery('#accordion div.pane:gt(0)').css('display', 'none');

or the less concise "notty firsty":

jQuery('#accordion div.pane:not(:first)').css('display', 'none');

and why not use hide instead of setting the style:

jQuery('#accordion div.pane:gt(0)').hide();

For better performance (and a concise solution), you might consider using JavaScript's Array.slice, which works on jQuery objects since they are arrayish:

jQuery('div').slice(1).hide();

Upvotes: 5

Kiley Naro
Kiley Naro

Reputation: 1769

You could give your first element a distinguishing ID or class, and use the jQuery not selector:

http://api.jquery.com/not-selector

Upvotes: 0

James Montagne
James Montagne

Reputation: 78630

jQuery('#accordion div.pane').not(':first').css('display', 'none');

Upvotes: 1

Related Questions