user405398
user405398

Reputation:

How to get UL's if child attribute using jquery?

i want to know how to get Unorderedlist's first child className. I know the UL's ID. How can i find it with jQuery?

Thanks in Advance!!

Upvotes: 0

Views: 311

Answers (4)

Adrian Gonzales
Adrian Gonzales

Reputation: 1010

@troynt made a valuable addition, adding the > in between the id of the ul and the li:first makes sure you only grab the first child of the ul you're targeting and not any li's within the child li's.

This will assure that you only get the first class name of the child LI, if there are multiple class names:

$('#id > li:first').attr('class').split(" ")[0]

That takes the class attribute, splits the string by spaces and returns the first element in the resulting array. If there is only one class name, it'll still work as expected.

Upvotes: 1

troynt
troynt

Reputation: 1912

$('#list > li:first').attr('class')

Upvotes: 5

Joseph
Joseph

Reputation: 25513

$("#ULID li:first").attr("class");

Also, keep in mind that your element can potentially have more than one class.

Upvotes: 1

rcravens
rcravens

Reputation: 8390

Give this a try:

$("#id li:first").attr("class");

where id=the UL's ID.

Upvotes: 5

Related Questions