Is there a way to have the following code written more efficiently?
Please note that this section of navigation is part of a CMS and is set to hidden hence you won’t see it expanded. I have the following code that works i.e re writes the nav items and adds a class of current according to the location.href but I was hoping it could be written more efficiently?
HTML
<ul>
<li><a class="current" href="#"><strong>Level 1</strong></a>
<ul class="sub">
<li><a class="" href="#">sub level</a></li>
<li><a class="" href="#">sub level</a></li>
<li><a class="" href="#">sub level</a></li>
</ul>
</li>
</ul>
jQuery
The first part rewrites the hidden section
$(document).ready(function () {
$('menu li:eq(4)').html('<ul><li><a href="/level1" class="current"><strong>level1</strong></a>'
+'<ul class="sub"><li><a href="/sub-level-a" class="">Sub Level A</a></li>'
+'<li><a href="/sub-level-b" class="">Sub Level B</a></li>'
+'<li><a href="/sub-level-c" class="">Sub Level C</a></li></ul></li></ul>');
});
Adds Class
$(document).ready(function () {
if(window.location.href.indexOf("/ucpoolvehicles")!=-1)
{
$("ul.sub li a:eq(0)").addClass("current");
} else {
$("ul.sub li a:eq(0)").removeClass("current");
}
if(window.location.href.indexOf("/car-rental")!=-1)
{
$("ul.sub li a:eq(1)").addClass("current");
} else {
$("ul.sub li a:eq(1)").removeClass("current");
}
if(window.location.href.indexOf("/private-vehicle-usage")!=-1)
{
$("ul.sub li a:eq(2)").addClass("current");
} else {
$("ul.sub li a:eq(2)").removeClass("current");
}
});
I hope I’m clear in with my instructions
Thanks in advance
This should work: