Solved

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

Posted on 2011-09-19
14
254 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now