Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery - Active Link and Parent Relationship
    text
    copied!<p>I am working on a navigation for a site and need some guidance on dynamically adding a class to the active link. In addition, once that link is established and I need to reference back to the parent and have it "show".</p> <p>This is what I am working with. The navigation is accordion style but does not use the Accordion UI. </p> <pre><code>&lt;ul id="menu3"&gt; &lt;li&gt;&lt;a href="{site_url}"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a class="drop" href="#"&gt;Information&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="{site_url}information/audio"&gt;Audio&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="{site_url}information/video"&gt;Video&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="{site_url}information/presentations"&gt;Presentations&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a class="drop" href="#"&gt;Clinics&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="{site_url}clinics/one"&gt;Office 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="{site_url}clinics/two"&gt;Office 2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="{site_url}forms/"&gt;Forms&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="{site_url}success_stories/index"&gt;Success Stories&lt;/a&gt;&lt;/li&gt; </code></pre> <p></p> <p>Then I have a bit of jQuery to initially hide the submenu items:</p> <pre><code>$(function(){ $('ul#menu3 ul').hide(); $('ul#menu3 &gt; li &gt; a.drop').click(function(){ $(this).parent().children('ul').toggle("slow"); return false; }); </code></pre> <p>So far so good. Everything works.</p> <p>Now I would like to dynamically highlight the active link and I tried using:</p> <pre><code>var path = location.pathname.substring(1); if ( path ) $('ul#menu3 a[href$="' + path + '"]').attr('class', 'selected'); </code></pre> <p>but it doesn't seem to be enough to add the correct class.</p> <p>The last thing I need to do is to have the navigation open to the active group. For example if <strong>Audio</strong> is the current page, the <strong>Information</strong> section of the navigation accordion would be open to show the active link.</p> <p>I would really appreciate some help on this. Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload