Solved

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

Posted on 2011-09-19
14
255 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 67
Basic JavaScript Question 3 38
Don't understand jquery promises 6 37
replace all occurrences within a string 8 17
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now