Solved

append <span> to <li> not working

Posted on 2010-09-23
7
548 Views
Last Modified: 2012-06-27
I have:

<ul class="sideMenu">
<li><a href="">News Item #2</a></li>
<li><a href="">News Item #1</a></li>
</ul>

I want this:

<ul class="sideMenu">
<li><span><a href="">News Item #2</a></span></li>
<li><span><a href="">News Item #1</a></span></li>
</ul>

My code (but doesnt work)

$("#sideMenu li").each(function()
{
            $("#sideMenu li").append($("</span>"));
});

Also, I would like to add 'class="act"' to the <li> tag for the active <a href>

My code (again not working)

$("#sideMenu li a").each(function()
{
     if(location.pathname.indexOf(this.href) > -1)
     {
       $("#sideMenu li").addClass("act");
     }
});
0
Comment
Question by:ellandrd
  • 4
  • 2
7 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33741874
change this
$("#sideMenu li").each(function()
{
            $("#sideMenu li").append($("</span>"));
});

to

$("#sideMenu li").each(function()
{
            $("#sideMenu li").append("</span>");
});


also this
$("#sideMenu li a").each(function()
{
     if(location.pathname.indexOf(this.href) > -1)
     {
       $("#sideMenu li").addClass("act");
     }
});

should be
$("#sideMenu li a").each(function()
{
     if(location.pathname.indexOf(this.href) != -1)
     {
       $(this).parent().addClass("act");
     }
});
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33741905
ohh, after reading the question again,
change
$("#sideMenu li").each(function()
{
            $("#sideMenu li").append($("</span>"));
});

to
$("#sideMenu li").each(function()
{
   var innerHTML = $(this).html();
   $(this).html("<span>" + innerHTML + "</span>" ));
});


0
 
LVL 16

Author Comment

by:ellandrd
ID: 33741906
Have you tested this? As it doesnt work for me...
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33741912
check the last reply also
0
 
LVL 16

Author Comment

by:ellandrd
ID: 33741925
Thank you!
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33741934
thanks for the points
0
 
LVL 22

Expert Comment

by:kadaba
ID: 33741992
Just few pointers:

$("#sideMenu li") should be  $(".sideMenu li"). since you have used it as a class and not an ID. It would work well if you have the Id for that (I dint see it in the post though)

$("#sideMenu li a").each(function()
{
      if(location.pathname.indexOf(this.href) != -1)
     {
        $(this).parent().addClass("act");
     }
});

would it work... it could if you use this function before changing the html to append span

So it could have been this way

$(".sideMenu > li > span > a").each(function()
{
       if(location.pathname.indexOf($(this).attr("href")) != -1)
     {
         $(this).parent().parent().addClass("act");
     }
});

Best,
kadaba
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
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…

860 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