views:

91

answers:

4

hello again

Such a simple concept but I'm struggling to express it ... apologies in advance for my verbosity.

I have a container div with a class, e.g., ; I want to use that class to do two things:

  1. add a class (e.g., 'active') to the nav element whose ID matches the class of div#container (e.g., #nav-primary li# apples)
  2. add the same class to another element if part of this element's ID matches the class of #container (e.g., div#secondary-apples)

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 contain the class of div#container?

I've tried a few variations of something like this:

<script type="text/javascript">
   $(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
</script>

The markup is, e.g.:

<div id="container" class="apples"> ...
   <ul id="nav-primary">
      <li id="apples"> ...
      <li id="oranges"> ...
      <li id="bananas"> ...
   </ul>



<div id="aux-left">
   <div id="secondary-apples"> ... 
   <div id="secondary-oranges"> ... 
   <div id="secondary-bananas"> ... 

Many thanks in advance for any suggestions, svs

A: 

Not sure if I understand your needs:

add a class (e.g., 'active') to the nav element whose ID matches the class of div#container (e.g., #nav-primary li# apples)

The # is for ids, not for classes. CSS classes are indicated using ., e.g. .myclass ! The expression li# doesn't make sense. Did you mean li #apples? In that case, you could leave the li out: there must only be a one element with id 'apples' anyway, so the additional li selector isn't helpful.

add the same class to another element if part of this element's ID matches the class of #container (e.g., div#secondary-apples)

Perhaps you're looking for the ~= and *= selectors? You could, for example select E[foo*="bar"]. Also make sure not be fooled by the .each method: jquery often operates on all matched elements anyways. The each method you provided is probably not what you want because it doesn't access this, nor the 'loop variable': the declaration isn't correct as far as I can see.

mnemosyn
thanks mnemosyn ... yeah, I wasn't clear enough about my question but my response above should clarify ... cheers!
shecky
+2  A: 

You don't need to loop through the nav elements with .each(). You just need the right jQuery selectors.

$(document).ready(function(){
    var containerClass = $('#container').attr('class');
    $('#nav-primary li.active').removeClass('active');
    $('#nav-primary li#'+containerClass).addClass('active');
    $('#aux-left div.active').removeClass('active');
    $('#aux-left div[id$='+containerClass+']').addClass('active');
});
munch
+1. Selectors do the "looping" for you. :)
ghoppe
A: 

Thanks to munch and mnemosyn.

Munch - outrageous! That simplicity is what I was after from the get-go but just couldn't cobble it together ... and btw, there's no 'active' class by default so I was able to trim 2 lines of your solution - a thing of beauty! ... and I was happy as a clam that I'd previously found this solution:

$(document).ready(function() {
    var containerClass = $('#container').attr('class');

    $('#nav-primary li').each(function(){
        var secondaryID = $(this).attr('id');
        if  ( secondaryID == containerClass )
            {
                $(this).addClass('active');
                $('#aux-left').find('div[id*="'+secondaryID+'"]').each(function(){
                    $(this).addClass('active');
                }); // end #aux-left.find
            }       // end if
    }); // end #nav-primary.each    
}); // end doc.ready.func

... but yours is even better!

Huge thank you!

cheers, svs

shecky
A: 

munch, if you'll indulge me in a couple of follow-up questions (I hope this is of interest to others).

Here's my real-world usage so far:

$(document).ready(function(){
    var containerClass = $('#main-content').attr('class');
    $('#nav-primary li#'+containerClass).addClass('active');
    $('#aux-left div[id$='+containerClass+']').addClass('active');
});

I've discovered that if there's no class in <div id="main-content">, then the 'active' class is added to all the #nav-primary LIs, and also to all the #aux-left DIVs; can you show me please how to modify this to account for such an occasion, i.e., if there's no class on #main-content, do nothing?

Also, how can I target only the first or second of multiple classes to store in the 'containerClass' variable, e.g., <div id="main-content" class="apples bananas">?

Very much appreciated! svs

shecky