Solved

I need to index through the li's for a given ul

Posted on 2011-09-19
14
257 Views
Last Modified: 2012-05-12
I have a ul which contains three or more li's. I need to index through and set the text.

What's the syntax for that?

Thanks!
0
Comment
Question by:newbieweb
  • 7
  • 3
  • 2
  • +1
14 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 167 total points
ID: 36561750
something like this

$("ul li").each(function(){
var index = $(this).index();

switch(index)
{
   case 0:
      $(this).html("zero");
   break;
   case 1:
      $(this).html("one");
   break;
   case 2:
      $(this).html("two");
   break;
   case 3:
      $(this).html("three");
   break;
}
 
});
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36561833
$("ul>li").each(function() {
    $(this).prepend( $(this).index("ul>li") );
})

Open in new window


test page : http://jsfiddle.net/Jvuk9/
0
 
LVL 3

Assisted Solution

by:mykde
mykde earned 167 total points
ID: 36561901
Syntax:
$("ul li").each(function() {
    $(this).prepend( 'Index = ' + $(this).index());
})

Open in new window


Output:
Index = 0
Index = 1
Index = 2
Index = 3
Index = 4
Index = 5

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:newbieweb
ID: 36564118
Thanks. But I was really thinking about the get() function or treating the selector as an array. I would like the zero'th element in one line. I'd like 0, 1, and 2 elements and use three lines to get them.

I thought it was easy but could not finc the syntax.


Any ideas on that?

0
 

Author Comment

by:newbieweb
ID: 36564173
Also, as an aside, please explain why this print two columns of numbers:

$("ul>li").each(function() {
    $(this).prepend( $(this).index("ul>li") );
})
$("ul>li").each(function() {
    $(this).prepend( $(this).index("ul>li") );
})

thusly:
11
22
33
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 166 total points
ID: 36564181
for each li you add the position/index of the li in the ul at the beggining, inside the li.
you do the same thing two times
0
 

Author Comment

by:newbieweb
ID: 36564187
any way you could explain that more clearly? I see it's the same code, I just ran it as a test.

I expected:
1
2
...
10
1
2
...
10

but am new to this. Where is the position referenced?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 166 total points
ID: 36564264
try this, it may help you understand : http://jsfiddle.net/Jvuk9/2/
0
 

Author Comment

by:newbieweb
ID: 36564272
I ran that on fiddle and became curious how the position was set, hence my question.

I will ask that in another question though. Let's get back to the main question: getting the selector at a given index.

such as .get(2) or [2] syntax.

Do you now how to do this?

0
 

Author Comment

by:newbieweb
ID: 36564279
0
 
LVL 3

Assisted Solution

by:mykde
mykde earned 167 total points
ID: 36564284
The reason is due to the function .prepend() which is adding the value to the beginning of the content in the li element.  When you run it again you are again adding the index number to the beginning of the content.
Start with:
<ul>
 <li></li>
 <li></li>
 <li></li>
</ul>

Open in new window

Run it once:
<ul>
 <li>0</li>
 <li>1</li>
 <li>2</li>
</ul>

Open in new window

Twice:
<ul>
 <li>00</li>
 <li>11</li>
 <li>22</li>
</ul>

Open in new window

Third time:
<ul>
 <li>000</li>
 <li>111</li>
 <li>222</li>
</ul>

Open in new window

0
 

Author Comment

by:newbieweb
ID: 36564291
I see. Prepend. That makes sense. But I should not have detracted from my primary question:

I really need to understand how to use .get(2) or the array syntax to pull an item out of a selector.
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 167 total points
ID: 36564891
if you just want to get li element at 0,1 and 2 index position, then why don't you simply use nth-child selector
http://api.jquery.com/nth-child-selector/
var li1 = $("ul li:nth-child(0)");
var li2 = $("ul li:nth-child(1)");
var li3 = $("ul li:nth-child(2)");

0
 

Author Closing Comment

by:newbieweb
ID: 36566157
Thanks!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question