I have a custom accordion menu with [+] link next to each expandable UL (toggle_action) to expand its child (div_toggle). For the active
Here’s my HTML:
<ul class="topnav">
<li><h3 class="toggle_action"><img class="img-swap" src="/images/plus.gif"/>Governance</h3>
<ul class="div_toggle">
<li><h3><img src="/images/spacer.gif"/><a href="/governance">Governance</a></h3></li>
<li><h3 class="toggle_action"><img class="img-swap" src="/images/plus.gif"/>Board of Directors</h3>
<ul class="div_toggle">
<li><h3><img src="/images/spacer.gif"/><a href="/governance/bod" class="active">Board of Directors</a></h3></li>
<li><h3><img src="/images/spacer.gif"/><a href="/governance/bod/min">Minutes</a></h3></li>
</ul>
</li>
<li><img src="/images/spacer.gif"/><a href="/governance/com">Committees</a></h3></li>
</ul>
</li>
<li><h3 class="toggle_action"><img class="img-swap" src="/images/plus.gif"/>About</h3>
<ul class="div_toggle">
<li><h3><img src="/images/spacer.gif"/><a href="/about">About Us</a></h3></li>
<li><h3><img src="/images/spacer.gif"/><a href="/about/contact">Contact</a></h3></li>
</ul>
</li>
</ul>
and the jQuery:
$('.topnav a.active').parents().attr('src').replace('plus.gif','minus.gif');
What am I doing wrong?? Thanks in advance!
The way you have your html nested, it will never grab the
<img>tag because it is not considered a parent of theactiveclass<img>tag.You should consider changing the way your HTML is being outputted most definitely.
But here’s a solution to your problem in the current way that you html is being nested.
Ok, what this does, is that it grabs the
"active"parents<h3>child<img>and adds a class to it called"plus"and uses this class when looping through the<h3 class="toggle_action"><img>tags, making sure to grab the img tag with plus as a class, and changes the other ones to the minus.gif image.Hope this helps you. If so, mark it as your answer.