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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:24:40+00:00 2026-06-02T04:24:40+00:00

If I wanted to hide all elements except for those within a <div id=content>

  • 0

If I wanted to hide all elements except for those within a <div id="content"> (including div#content itself), I could use the following CSS:

*
{
    visibility: hidden !important;
}

div#content, div#content *
{
    visibility: visible !important;
}

One thing to note about this solution is that the hidden elements still take up space. Unfortunately, not all elements have the same display attribute, so you cannot simple simply replace visibility above with display.

Using JavaScript, how can I set all elements to that are not within the <div id="#content"> ‘family’ to display: none?

  • 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-06-02T04:24:43+00:00Added an answer on June 2, 2026 at 4:24 am

    A general purpose solution to change the style on the fewest objects, but make sure that #content and all it’s sub-elements are visible requires an algorithm to traverse up from #content and hide all siblings at each level up without ever hiding an ancestor of #content. Because this starts at #content and goes up, it never hides any elements inside of #content.

    function hideAllExcept(id) {
        var el = document.getElementById(id);
        while (el && el != document.body) {
            // go one level up
            var parent = el.parentNode;
            // get siblings of our ancesotr
            var siblings = parent.childNodes;
            // loop through the siblings of our ancestor, but skip out actual ancestor
            for (var i = 0, len = siblings.length; i < len; i++) {
                if (siblings[i] != el && siblings[i].nodeType == 1) {
                    // only operate on element nodes
                    siblings[i].style.display = "none";
                }
            }
            el = parent;
        }
    }
    
    hideAllExcept("content");
    

    Caveat: this first version does not hide text nodes that are siblings of an ancestor of #content (all other text nodes outside of #content are hidden because their parent is hidden). To hide those text nodes too, they would have to get wrapped in a <span> tag so the style could be set on the <span> tag, but I don’t know if the OP needs that level of complexity or wants the text nodes wrapped in that way.

    For completeness, here’s a version that will wrap parent sibling text nodes so they can also be set to display: none. This may or may not be needed depending upon the source HTML:

    function hideAllExcept(id) {
        var el = document.getElementById(id);
        while (el && el != document.body) {
            // go one level up
            var parent = el.parentNode;
            // get siblings of our ancesotr
            var siblings = parent.childNodes;
            // loop through the siblings of our ancestor, but skip out actual ancestor
            for (var i = 0, len = siblings.length; i < len; i++) {
                var node = siblings[i];
                if (node != el) {
                    if (node.nodeType == 1) {
                        // only operate on element nodes
                        node.style.display = "none";
                    } else if (node.nodeType == 3) {
                        // wrap text node in span object so we can hide it
                        var span = document.createElement("span");
                        span.style.display = "none";
                        span.className = "xwrap";
                        node.parentNode.insertBefore(span, node);
                        // Be wary of the dynamic siblings nodeList changing 
                        // when we add nodes.  
                        // It actually works here because we add one 
                        // and remove one so the nodeList stays constant.
                        span.appendChild(node);
                    }
                }
            }
            el = parent;
        }
    }
    
    hideAllExcept("content");​
    

    And a working demo: http://jsfiddle.net/jfriend00/yVWDx/

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

Sidebar

Related Questions

I asked a similar question here where I wanted to get a single div
If I wanted to hide(or change visibility, color, etc) of an element in WPF
I wanted to be able to hide part of my application window to display
I have a big item list that I wanted to hide, and then a
I wanted to know if is it possible to hide the microphone button (speech-to-text)
i'm struggling with display/hide text using javascript here. Here is what i wanted to
I wanted to hide the main window of my app on startup, so I
Wanted to know if there's a way to change a form field's visibility within
I'm programming my website and I wanted to create different css for different resolutions.
Wanted to know if someone had a suggestion on code or maybe there's a

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.