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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:45:01+00:00 2026-05-18T05:45:01+00:00

How to hide all elements except one div and its child element using jquery?

  • 0

How to hide all elements except one div and its child element using jquery?

  • 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-18T05:45:02+00:00Added an answer on May 18, 2026 at 5:45 am

    Ignore that, does not appear to have the desired effect.

    Perhaps the following?

    $('body').children().hide();
    $('#mydiv').children().andSelf().show();
    

    Update

    The problem is, the visible state of a child DIV often relies on it’s parent being visible. So you need to have an entire tree down to the DIV you want remaining visible.

    You need to hide everything apart from that tree, the trick is identifying all children of the DOM structure that are not in the DIV ancestry, and all siblings of the ones that are.

    Finally!

    Managed to write a solution. Tried a recursive approach at first, but on complicated pages it died with “too much recursion”, so wrote a list-based version that works just fine. It is in the form of a single-function jQuery plugin, as that seems to be the most useful approach.

    (function($) {
        $.fn.allBut = function(context) {
            var target = this;
            var otherList = $();
            var processList = $(context || 'body').children();
    
            while (processList.size() > 0) {
                var cElem = processList.first();
                processList = processList.slice(1);
    
                if (cElem.filter(target).size() != target.size()) {
                    if (cElem.has(target).size() > 0) {
                        processList = processList.add(cElem.children());
                    } else {
                        otherList = otherList.add(cElem);
                    }
                }
            }
    
            return otherList;
        }
    })(jQuery);
    

    This routine finds all elements within the context (which defaults to <body>) that exclude the object and it’s ancestors.

    This would achieve the result of the question:

    $('#mydiv').allBut().hide();
    

    Another use could be to retain all objects within a container with a certain class, fading out all the others:

    $('.keep').allBut('#container').fadeOut();
    

    Bare in mind that the layout and positioning of any given element can depend heavily on surrounding elements, so hiding something like that is likely to alter the layout unless things are absolutely positioned. I can see uses for the routine anyhow.

    However!

    Another poster came up with the following which returns the same information using a routine already built-in to jQuery which I had not seen.

    target.add(target.parentsUntil(context)).siblings();
    

    or without the variable

    target.parentsUntil(context).andSelf().siblings();
    

    Which is a tad simpler, must scour documentation in future.

    PS. If anyone has a better way of checking if a given jQuery object is equivalent to another than a.filter(b).size() != b.size() I would be very glad to hear it!

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

Sidebar

Related Questions

I need to hide() all class level elements of '.dd-container' except when that element
I am using jquery addClass code to show/hide elements when rolling over a div.
How do I hide a child in a accordion? Using visible doesn't seem to
I am trying to create a menu panel in jQuery, all is complete except
I am trying to hide the button based on the user's role using the
I'd like to hide a div when user click anywhere on the page outside
i have <ul> <li class=section-title>HEADER which triggers dropdown</li> <li><a href=element_one>element one</a></li> <li><a href=element_one>element one</a></li>
I use Jquery in my page to hide buttons which i dont want to
I want only one drawer to be open at startup. At the moment, all
I want to hide all the descendents of the ul for my tree menu

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.