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 743167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:49:53+00:00 2026-05-14T08:49:53+00:00

I’m using this bit of jQ to add a class to two different elements

  • 0

I’m using this bit of jQ to add a class to two different elements based on the class of another (parent/grandparent, etc) element:

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

Brilliant, but I have two problems with it:

First, when there’s no class at all in <div id="main-content">, the ‘active’ class is added to all the #nav-primary LIs, and also to all the #aux-left DIVs; how can I modify this so that in the absence of any class on #main-content, do nothing?

Second, 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

  • 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-14T08:49:54+00:00Added an answer on May 14, 2026 at 8:49 am

    For 1: do an if-clause after your first line, e.g. like this:

    if(containerClass.length > 1) {
       //your addClass code
    }
    

    //EDIT: The above would be placed after you define containerClass in your script. It just checks if the container has a class or not, by measuring the content of the variable (.length). If containerClass = “”, this if-condition will be false, and the contained code will not execute. Hence it should prevent your first problem from happening: that the “active” class is added to all its children, regardless of #main-content having a class or not

    For 2:

    classes = containerClass.split(' '); //split by space
    if(classes[1]) { //if there is a 2nd class
       //do something if there are multiple classes
       //access the 1st class with classes[0], etc.
    } else {
       //do something if #main-content only has 1 class
    }
    

    //EDIT: in the above, the first line splits whatever the variable containerClass contains by an empty space. You can compare that with php’s explode function: Say your container has 2 classes (class=”aplles bananas”), what you’ll end up with after splitting that by empty space is an array that contains both of these classes like this:

    classes[0] = 'apples';
    classes[1] = 'bananas';
    

    So now you have your 1st and 2nd classes, if they exist.

    The next line in my code would then do something if #main-content has a 2nd class (here: “bananas”), but you could also check against the first class with

    if(classes[0]) { ...
    

    The “else” part in my code would trigger if #main-content doesn’t have a 2nd class. You could expand the above if-else-condition by first checking if a 1st class exists, then checking if a 2nd class exists, and then doing something else (or nothing) if #main-content doesn’t have any classes at all. I hope this makes it clearer.

    Hope this helps. On an unrelated note, you might want to add “jquery” to the tags of this post so that more people can find it 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 411k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are trying to display the UIImagePickerController from your view… May 15, 2026 at 7:41 am
  • Editorial Team
    Editorial Team added an answer There's a tutorial on the Netbeans site about using JPA… May 15, 2026 at 7:41 am
  • Editorial Team
    Editorial Team added an answer You want to be careful when using a CopyOnWriteArrayList. The… May 15, 2026 at 7:41 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.