$(document).ready(function(){
$("#menu ul").children('li').each(function(){
var href = $(this).find('a').attr('href');
var wpl = window.location.pathname;
if( href.indexOf(wpl) != -1 ){
if( wpl.indexOf( "category" ) != -1){
$("#products").attr('id','prodon');
}
if( wpl.indexOf( "news" ) != -1){
$("#news").attr('id','newson');
}
if( wpl.indexOf( "wheretobuy" ) != -1){
$("#WTB").attr('id','WTBon');
}....
It goes on a little longer, different buttons.
I’ve only done it this way because each button is an image so they’re all unique. This is a snippet of the HTML:
<li><a href="/news/index" id="news"></a></li>
<li>
<a href="/site/wheretobuy" id="WTB"></a>
<ul>
<?php echo '<li>'.CHtml::link(CHtml::encode("Showrooms"), array('/showroom/index')).'</li>'; ?>
<?php echo '<li>'.CHtml::link(CHtml::encode("Dealers"), array('/dealers/index')).'</li>'; ?>
<?php echo '<li>'.CHtml::link(CHtml::encode("Hypermarket and Merchants"), array('/hyper/index')).'</li>'; ?>
</ul>
</li>
So the parent nodes are all fine. Also, if a child’s URL contains the parent’s URL (i.e. category/cat), it shows the active button. I want to know how to apply a parent;s active img if one of its children is the URL, but not the same wording. I could do a bunch of more IFs but I think there’s an easier way.
EDited…. again
if( wpl.indexOf( "wheretobuy" ) != -1 || $('#menu ul').find('.WTBon').parent().closest('li').indexOf( "wheretobuy" ) != -1 ) {
$('.WTB').addClass('WTBon');
}
So what I’m trying to check is if the parent of the node has wheretobuy in the URL
Try this:
instead adding ids
add as class name.updates for latest one:
Note:
Using same ids for multiple elems on a single page is discouraged. Its not a good practice.
This applies in your case toobecauseyou have applied it by making the active one as id, You canuse it as a class instead.