Solved

append <span> to <li> not working

Posted on 2010-09-23
7
545 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

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

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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 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)

809 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