?
Solved

append <span> to <li> not working

Posted on 2010-09-23
7
Medium Priority
?
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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:
Gurvinder Pal Singh earned 2000 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33741912
check the last reply also
0
 
LVL 16

Author Comment

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

Expert Comment

by:Gurvinder Pal Singh
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

752 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