ellandrd
asked on
append <span> to <li> not working
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.index Of(this.hr ef) > -1)
{
$("#sideMenu li").addClass("act");
}
});
<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.index
{
$("#sideMenu li").addClass("act");
}
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Have you tested this? As it doesnt work for me...
check the last reply also
ASKER
Thank you!
thanks for the points
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.index Of(this.hr ef) != -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.index Of($(this).attr("href")) != -1)
{
$(this).parent().parent().addClass("act");
}
});
Best,
kadaba
$("#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.index
{
$(this).parent().addClass(
}
});
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.index
{
$(this).parent().parent().addClass("act");
}
});
Best,
kadaba
$("#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.index
{
$("#sideMenu li").addClass("act");
}
});
should be
$("#sideMenu li a").each(function()
{
if(location.pathname.index
{
$(this).parent().addClass(
}
});