Solved

Counting number of list items in unorderd list and adding count

Posted on 2011-09-06
2
188 Views
Last Modified: 2012-08-13
Hi

I have an unordered list that is genereated dynamically. It looks similar to the attached code.

Within each list item there is a link and i want to add a number to the querystring in the link, so for example the first link would read post.html?s=2011-07-04-channelports-soft-launch&tid=1
(note the querystring tid=1)
so the second link would have 2 added to read tid=2, third link tid=3 and so on..

Any ideas how to do this with javascript?

thanks


<ul id="mycarousel" class="jcarousel-skin-portfolio">
  <li><a href="post.html?s=2011-06-29-riverside-brand-comms&tid="><img src="/newsite/perch/resources/1315292504riversidethumb.jpg" alt="Riverside Brand Comms" align="left" border="0" /></a><span>List 1</span></li>
  <li><a href="post.html?s=2010-07-20-annual-awards&tid="><img src="/newsite/perch/resources/1315292570awardsthumb.jpg" alt="Annual Awards" align="left" border="0" /></a><span>List 2</span></li>
  <li><a href="post.html?s=2011-07-04-channelports-soft-launch&tid="><img src="/newsite/perch/resources/1315292491cpthumb.jpg" alt="ChannelPorts Soft Launch" align="left" border="0" /></a><span>List 3</span></li>
</ul>

Open in new window

0
Comment
Question by:coolispaul
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36491346
You may use :
$("li a","#mycarousel").each(function(i,el) {
    //var href = $(this).attr("href") + i;  // start with tid=0
    var href = $(this).attr("href") + (i + 1); 
    $(this).attr("href", href); 
    // comment or delete this line (testing purpose)
    alert( $(this).attr("href") );
})

Open in new window


test page : http://jsfiddle.net/g6VBb/
0
 

Author Comment

by:coolispaul
ID: 36491372
perfect!

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

914 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

16 Experts available now in Live!

Get 1:1 Help Now