Note that there are some explanatory texts on larger screens.

plurals
  1. POmatching an element's class to another element's ID, or *part* of another element's ID
    primarykey
    data
    text
    <p>Such a simple concept but I'm struggling to express it ... apologies in advance for my verbosity. </p> <p>I have a container div with a class, e.g., ; I want to use that class to do two things:</p> <ol> <li>add a class (e.g., 'active') to the nav element whose ID matches the class of div#container (e.g., #nav-primary li# apples)</li> <li>add the same class to another element if <em>part</em> of this element's ID matches the class of #container (e.g., div#secondary-apples)</li> </ol> <p>I assume there's an .each() loop to check the primary nav's list items' IDs, and to check the div IDs of the secondary nav ... though the latter needs to have its prefix trimmed ... or should I say more simply if the secondary nav div IDs <em>contain</em> the class of div#container?</p> <p>I've tried a few variations of something like this:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $('#nav-primary li').each(function(){ var containerClass = $('#container').attr('class'); var secondaryID = $('#nav-primary li').attr('id'); // something like if ('#nav-primary li id' == (containerClass) { } // or should I first store a variable of the LI's ID and do something like this: if ( secondaryID == containerClass ) { } // and for the trickier part, how do I filter/trim the secondary nav div IDs, something like this: var secondaryNavID = $('#aux-left div[id ... something here to strip the 'secondary-' bit ... ]'); }); // end each }); // end doc.ready.func &lt;/script&gt; </code></pre> <p>The markup is, e.g.:</p> <pre><code>&lt;div id="container" class="apples"&gt; ... &lt;ul id="nav-primary"&gt; &lt;li id="apples"&gt; ... &lt;li id="oranges"&gt; ... &lt;li id="bananas"&gt; ... &lt;/ul&gt; &lt;div id="aux-left"&gt; &lt;div id="secondary-apples"&gt; ... &lt;div id="secondary-oranges"&gt; ... &lt;div id="secondary-bananas"&gt; ... </code></pre> <p>Many thanks in advance for any suggestions, svs</p>
    singulars
    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.
    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