Skip to content

Getting the first item and nth item of a ul list in JQuery

January 4, 2016

Today, I had to brush up my JQuery skills as I had to utilise interactive ul lists in highlighting an item with a class name.

If you need to grab the first LI’s ID in some way and add active to its class you need to simply use :-

$(‘ul#someListID li:first).addClass(‘active’);

This adds the additional class name ‘active’ to the first Li item in the ul list.

To add to say the fifthitem or nth item, you can use li:nth-child instead like so:-

$(‘ul#someListID li:nth-child(5)).addClass(‘active’);

It is simple as that.


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: