Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 734083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:19:52+00:00 2026-05-14T07:19:52+00:00

Such a simple concept but I’m struggling to express it … apologies in advance

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:19:53+00:00Added an answer on May 14, 2026 at 7:19 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like such a simple issue but I cannot find an elegant solution.
This is such a simple problem, but I can't find an answer anywhere... I
this must be such a simple problem but can someone tell me why this
sorry this is such a simple question but I can't figure it out. How
This seems like such a simple question. I have several Edit boxes on my
I feel somewhat foolish asking such a simple question, but I can't seem to
In plain English, how is $this used in PHP? It's such a simple concept
I have a question that I think might have a simple answer but I
I'm embarrassed to ask such a simple question. My term does not start for
Before you start laughing at such a simple question let me explain: I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.