2

I have this:

        <span class="mad">2</span> Stuff

</li>

I want this

            <a href="/My/Website"><span class="mad">2</span> Website</a>

    </li>

Can someone give me some jquery to do this? Also "/My/Website" has to be changed to something different for each li...is this possible?

1
  • It is all possible, but you are not clear on how it sill be different for each li Commented Feb 9, 2011 at 21:20

3 Answers 3

1

Using each, you can give each li span.mad a different website url

$("li span.mad").each(function(i,e) {
 $(e).html(
    "<a href='/My/Website/"+ (i+1) +"'>"+$(e).html()+"</a>");
});

http://jsfiddle.net/6EzDs/

(or using wrap)

$("li span.mad").each(function(i,e) {
 $(e).wrap("<a href='/My/Website/"+ (i+1) +"' />");
});
Sign up to request clarification or add additional context in comments.

2 Comments

I need to add the span tag first to the li...then do this process how do I add a new span tag and class to each li first?
@nick edit your question if it does not start with <li><span>.. That's my interpretation of what the starting point is.
1

Give each li a unique class name and use the wrapp function

$('span .class1').wrap('<a href="class1 url"/>')

Comments

1
$("span.mad").wrap("<a href='/My/Website' />");

Or, if you want to go by id instead of class name

$("#someSpanID").wrap("<a href='/My/Website' />");

2 Comments

Can I iterate through the li's and add something different for '/My/Website'
Yes - see @cyberkiwi's answer below

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.