Note that there are some explanatory texts on larger screens.

plurals
  1. POMoving up multiple parents in jQuery - more efficient way?
    primarykey
    data
    text
    <p>So, I have a navigation that is a list and has sublists and sublists.</p> <p>Basically, the nav is by default collapsed, but if people click on a page that's in a sublist, I want to show the parent list. And if it's a sublist of a sublist, I need both parent lists to show. I have it set up, but, I don't like putting 5 .parent().parent() 's to traverse upward to expand the list. Is there a more efficient way?</p> <p>the HTML:</p> <pre><code>&lt;div id="lesson-sidebar"&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="index.html"&gt;Welcome to Beat Basics and Beyond&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="table-of-contents.html"&gt;What's in this course?&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="defining-your-beat.html" class="active"&gt;Defining Your Beat&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="boundaries-of-your-beat.html"&gt;Boundaries of Your Beat&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="the-beat-description.html"&gt;The Beat Description&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="build-your-own-beat-description.html"&gt;&lt;span class="ital"&gt;Activity:&lt;/span&gt; Build Your Own Beat Description&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="getting-started.html"&gt;Getting Started&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="debrief-your-predecessor.html"&gt;Debrief Your Predecessor&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="predecessor-top-five-tips.html"&gt;&lt;span class="ital"&gt;Activity:&lt;/span&gt; List The Top Five Tips From Your Predecessor&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="covering-your-beat-with-the-internet.html"&gt;Covering Your Beat With The Internet&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="get-in-the-car-and-go.html"&gt;Get in the Car and Go&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="mapping-your-beat.html"&gt;Mapping Your Beat&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="read-the-clips.html"&gt;Read the Clips&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="activity-dissect-this-clip.html"&gt;&lt;span class="ital"&gt;Activity:&lt;/span&gt; Dissect This Clip&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="writing-your-first-article.html"&gt;Writing Your First Article&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="starting-cold-on-the-beat.html"&gt;Starting Cold on the Beat&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="working-with-sources.html"&gt;Working With Sources&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="finding-sources.html"&gt;Finding Sources&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="diversify-your-sources.html"&gt;Diversify Your Sources&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="prospecting-for-stories-and-sources.html"&gt;Prospecting for Stories and Sources&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="building-relationships.html"&gt;Building Relationships&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="going-off-the-record.html"&gt;Going Off the Record&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="developing-resources.html"&gt;Developing Resources to Help You on the Beat&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="develop-a-calendar-of-events.html"&gt;Develop a Calendar of Events&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="build-your-library.html"&gt;Build Your Library&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="learn-the-open-record-laws.html"&gt;Learn the Open Record Laws&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="extra-resources.html"&gt;Extra Resources&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="beat-breakdown-tool.html"&gt;Beat Breakdown Tool&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="links-library.html"&gt;Links Library&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="general-resources-for-any-beat.html"&gt;General Resources for Any Beat&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="courts-and-criminal-justice-links.html"&gt;Courts and Criminal Justice Links&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="education-resources.html"&gt;Education Resources&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="local-government-links.html"&gt;Local Government Links&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="neighborhood-or-suburban-links.html"&gt;Neighborhood or Suburban Links&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="police-and-public-safety-links.html"&gt;Police and Public Safety Links&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="reporter-organizations.html"&gt;Reporter Organizations&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="additional-reading.html"&gt;Additional Reading&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="final-thoughts.html"&gt;Final Thoughts&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>The jQuery:</p> <pre><code>function toggleSubmenu() { if ($(this).hasClass('submenu-hidden')) { $(this).parent().children('ul').slideToggle(); $(this).removeClass().addClass('submenu-visible'); } else if ($(this).hasClass('submenu-visible')) { $(this).parent().children('ul').slideToggle(); $(this).removeClass().addClass('submenu-hidden'); } } $('#lesson-sidebar ul ul').hide(); $('#lesson-sidebar ul ul ul').hide(); $('#lesson-sidebar ul:first-child').attr('id', 'rootlist'); $('#lesson-sidebar ul li:has("ul")').prepend('&lt;span class="submenu-hidden"&gt;&lt;/span&gt;').css('list-style','none'); $('#lesson-sidebar ul li a').each( function() { if ($(this).hasClass('active')) { // if it is a UL var length = $(this).parent().find("ul").length; alert(length); if (length == 0) { if ($(this).parent().parent().parent().children('span').hasClass('submenu-hidden')) { $(this).parent().parent().parent().children('ul').show(); $(this).parent().parent().parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible'); } if ($(this).parent().parent().parent().parent().parent().children('span').hasClass('submenu-hidden')) { $(this).parent().parent().parent().parent().parent().children('ul').show(); $(this).parent().parent().parent().parent().parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible'); } } if (length == 1) { $(this).parent().find('ul').slideToggle(); $(this).parent().children('span').removeClass('submenu-hidden').addClass('submenu-visible'); } } } ); $('ul#rootlist &gt; li span, ul#rootlist li ul li &gt; span').bind('click', toggleSubmenu); </code></pre> <p>Any and all help is majorly appreciated. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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