Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  150] [ 4]  / answers: 1 / hits: 18110  / 8 Years ago, sat, april 9, 2016, 12:00:00

I have a ul in div. And I have another ul inside li. I need to select only first ul which located inside the div. How to achieve that in jquery.



The markup.



<div class=parent>
<div class=clearfix>
<div class=another-div>
<ul class=first-ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
<li>Fifth</li>
<li>
<ul class=second-ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
<li>Fifth</li>
</ul>
</li>
</ul>
</div>
</div>
</div>


I want to select the first-li using only parent class. I have tried $('.parent > ul') but this is not working. and $('.parent ul') but this selects both ul. I don't want to use first-ul class or anything.


More From » jquery

 Answers
114

You can access the first ul in the following way also:



$('.parent ul:first li:first').css('color', 'blue');


ul:first will select the first ul in div with class as parent



Check this jsfiddle.


[#62641] Wednesday, April 6, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaleelh

Total Points: 661
Total Questions: 125
Total Answers: 103

Location: Sweden
Member since Mon, May 8, 2023
1 Year ago
;