Solved

append <span> to <li> not working

Posted on 2010-09-23
7
557 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: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
Industry Leaders: 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!

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

707 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