Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
132
rated 0 times [  136] [ 4]  / answers: 1 / hits: 16305  / 14 Years ago, tue, january 4, 2011, 12:00:00

Can I work with the index value so easily? I think using the natural index values is better then using classes. I want to use the .index in that way.



Html



<div id=test>
<ul>
<li>Im index 0</li>
<li>Im index 1</li>
<li>Im index 2</li>
<li>Im index 3</li>
</ul>
</div>


Pseudo (Jquery) Javascript



$('#test ul > li').index(2).hide();

$('#test ul > li').index(1).click(function() {
alert('lulz you clicked the li with the index value of 1 bro');
});


I doesnt find a clue how to work this way with the .index value.. is it possible to work that easily with this method??


More From » jquery

 Answers
92

You can use eq:



$('#test ul > li').eq(2).hide();


It can also be a part of your selector:



$('#test ul > li:eq(2)').hide();


If you want the third li in all uls in #test, you can use nth-child (note it is 1-based):



$('#test ul > li:nth-child(3)').hide();

[#94391] Saturday, January 1, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nora

Total Points: 248
Total Questions: 111
Total Answers: 97

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
;