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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:24:23+00:00 2026-05-27T13:24:23+00:00

What I have is 3 divs of the same class but with a unique

  • 0

What I have is 3 divs of the same class but with a unique id and when I click one box I would like to hide the other 2.

This isn’t a problem and I could achieve it with a few if/elses or a case statement perhaps but I was wondering if there was a more generic/efficient way to hide all elements of the same class that isn’t the one that was clicked?

<div id="boxes">
    <div id="box1" class="box">Box 1</div>
    <div id="box2" class="box">Box 2</div>
    <div id="box3" class="box">Box 3</div>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $('.box').click(function() {
        $(this).html('hi');
        if ($(this).attr('id') == 'box1')
        {
            $('#box2').hide('slow');
            $('#box3').hide('slow');
        }
       .......... more if's     
    });
});
</script>
  • 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-27T13:24:24+00:00Added an answer on May 27, 2026 at 1:24 pm

    You can use the .not filter function to refer to “the other” boxes. This way the code will work even if the boxes are not necessarily siblings in the DOM tree, or if there are some additional siblings that are not boxes.

    var $boxes = $('.box');
    $boxes.click(function() { 
        $(this).html('hi'); 
        $boxes.not(this).hide('slow');
    }); 
    

    Notice that I ‘m caching the result of $('.box') for performance — doubtless it will not be noticeable, but there’s no reason not to do that.

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

Sidebar

Related Questions

i have two divs with the same ids but one has display=none and the
I have divs with same class, but each 3 are wrapped in a parent
I have four divs each with the same ID but different class. <div id=inner
I have multiple divs in my body which need the same style: <div class=box></div>
I have a group of hidden divs all with the same class but with
I'm trying to toggle hide/show on all divs with the same class. I have
My issue is that I have two (or more) divs of the same class,
I have 30 divs with the same class on the same page. When i'm
I have a page containing several divs with the same class name for CSS
I have 1 div that will contain 3-5 divs with the same class. Below

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.