I have an accordion menu that the active link drops down the menu, loads an external page into a div, then changes the banner on the same click. I have everything working except for the banner image changing out. I thought I had the code right, but something seems to be missing, and I can’t figure it out.
Here is my html:
<section id="commercialBanner">
<img src="images/catalog/indoorBanner.jpg"/>
</section>
<section id="accordionNav">
<ul id="nav">
<li id="indoorEntrance" class="category"><a class="ext" id="indoor" href="montage.html">Indoor Entrance</a>
<ul id="indoorEntranceSubmenu" class="sideSub">
<li><a href="montage.html" class="ext">Ecomat Squares</a></li>
<li><a href="gatekeeper.html" class="ext">Gatekeeper</a></li>
<li><a href="absorba.html" class="ext">Absorba</a></li>
</ul>
</li>
<li id="outdoorEntrance" class="category"><a id="outdoor" class="ext" href="aquaFlow.html">Outdoor Mats</a>
<ul id="outdoorEntranceSubmenu" class="sideSub">
<li><a href="aquaFlow.html" class="ext" id="aquaflow">AquaFlow</a></li>
</ul>
</li>
</ul>
<section id="catalog"></section>
Here is my Script:
$('a.ext').click(function (event){
event.preventDefault();
$('#catalog').load(this.href);
});
$('.category > a').click(function(evt) {
if ($(this).attr('class') != 'active'){
$('#nav li ul').hide();
$(this).next().show()
// $(this).next('li > a').trigger('click')
$('#nav li a').removeClass('active');
$(this).addClass('active');
};
if ($('#indoor').attr('class') == 'active'){
$("#commercialBanner img").attr("src", "images/catalog/indoorBanner.jpg");
};
if ($('#outdoor').attr('class') == 'active'){
$("#commercialBanner img").attr("src", "images/catalog/outdoorBanner.jpg");
};
});
You seem to be checking the class attribute incorrectly – you have more than one class on an element, so:
As noted by @epascarello: use hasClass(“active”).
See this:
jQuery's attr – return multiple classes not just the 1st