Conceal all components on a webpage with the exception of a designated div

I am looking to selectively hide elements on the page, revealing only the contents of div.k1. The page contains numerous elements. How can I achieve this using just CSS?

<div>1-this will be hidden</div>
<div class="k1">
  2-this div will be shown
  <p>3-this will be shown</p>
  <p>4-this div will be shown</p>
  <div>
    5-this will be shown
    <p>6-this will be shown</p>
    <div>
      7-this will be shown
      <p>8-this will be shown</p>
    </div>
  </div>
</div>
<div>9-this will be hidden</div>
<div>10-this will be hidden</div>

<div>1-this will be hidden</div>
<div class="k1">
  2-this div will be shown
  <p>3-this will be shown</p>
  <p>4-this div will be shown</p>
  <div>
    5-this will be shown
    <p>6-this will be shown</p>
    <div>
      7-this will be shown
      <p>8-this will be shown</p>
    </div>
  </div>
</div>
<div>9-this will be hidden</div>
<div>10-this will be hidden</div> 11,12,13..

Answer №1

To hide all the specific elements within the body tag that are div's, excluding those with the class name k1, you can utilize the following JavaScript code snippet.

var items = document.querySelectorAll("body>div:not(.k1)");
for (var i = 0; i < items.length; i++) {
    items[i].style.display = 'none';
}

This script selects all the div tags directly under the body tag, except for those with the class name k1. It then loops through these selected elements and hides each one by setting its display property to 'none'.


If a CSS approach is preferred and the criteria remain the same as described above, you can achieve the same result using the following CSS rule.

body>div:not(.k1) {
    display: none;
}

If you wish to delve deeper into CSS selectors, I recommend exploring the topic on the W3 schools page.

Answer №2

If you're looking for a quick solution to hide divs nested up to 2 layers deep, here's a simple approach. By targeting all div elements and checking their classNames and parent classNames, we can selectively hide them based on specified conditions. This method involves adding multiple conditional checks to ensure only the desired elements remain visible. For more complex nesting structures, additional parentNode references can be included in the script. However, it may be more efficient to assign specific classes for hiding elements and revealing others using IDs. This alternative strategy avoids modifying the original HTML structure.

Cheers!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Could anyone provide some insight into the reason behind this occurrence?

I just came across the most peculiar situation I've ever experienced. Check out this unique test page: <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title></title> <script language=javascript> fun ...

Could there possibly exist a counterpart to .cloneNode?

I'm currently working on a recipe website submission form, and I've successfully implemented code that allows me to add more ingredients dynamically. addIngredientsBtn.addEventListener('click', function(){ let newIngredients = ingre ...

What is the process for adjusting the switch size in react-bootstrap?

I've been struggling to increase the size of the react-bootstrap switch without success. I searched through the documentation and tried various methods, but couldn't find a solution other than directly modifying the bootstrap css. Here's the ...

Modify the hue of the div as soon as a button on a separate webpage is

Looking for assistance with a page called "diagnosticoST" that contains four buttons (btn-institucional, btn-economico, btn-social, btn-natural). These buttons have different background colors until the survey inside them is completed. Once the user comple ...

Unchanging bottom section with changeable information

I have a unique website design in mind that involves using an image as the background and placing the content within a box. View my website design here However, I am facing an issue with a specific page on this website. The page contains accordion buttons ...

Tips on customizing the appearance of JavaScript output?

I recently created a plugin for my website with JavaScript, and one of the lines of code I used was output.innerHTML = "Test"; Is it possible to apply CSS styles to this element, or is there an alternative method? ...

What are the steps to create a dynamic dropdown effect for navbar content using animations similar to transitions?

IMPORTANT: THE NAVIGATION BAR IS NOT USING BOOTSTRAP When I hover over the "More" button, the dropdown content appears suddenly and disappears abruptly when moving the mouse away. I would like to add a transition effect to make the dropdown content appear ...

Troubleshooting issue with image dimensions in Angular within CKEditor content

One issue I am facing is with CKEditor, where images inserted into Rich Text Fields have their height and width attributes set in a CSS style tag. For example: <img alt="" src="https://something.cloudfront.net/something.jpg" style="height:402px; ...

Display a particular div upon clicking a specific link, while concealing the other divs

As a beginner in the world of jQuery and coding, I'm encountering some difficulties that I need help with. My goal is to have the 'Vlogging' link activated and show 'Details 1' when the page loads. Then, upon clicking on either &a ...

Utilizing CSS flex-end to position images

I am facing an issue with aligning the last item in my container named section-a. The height of the container is set to 100vh and it contains 3 items. I have tried using the property align-self:flex-end, but there are no visible changes. Can anyone help me ...

I am experiencing difficulty with my grid as it refuses to center elements

Hello everyone, this is my first post here. If I missed any important information or didn't present things correctly, please feel free to let me know. I'm currently a developer in training and recently had to work on my first website project aft ...

Tips for showing all percentages on a Google PieChart

I'm currently encountering two issues. How can I ensure that the entire legend is visible below the graph? Sometimes, when the legend is too large, three dots are added at the end. Another problem I am facing involves pie charts. Is there a way to d ...

Add color to the cells within the table

I need help with dynamically adding colors to my table based on the results. If the result is 'UnSuccessfull', the color should be 'red'. If the result is 'Successful', the color should be 'green'. The challenge I a ...

What is the best way to eliminate excess elements from overflow:hidden?

Whenever I click on a modal, it changes my body to overflow:hidden. As a result, the scrollbar disappears and my page becomes unscrollable. Once I close the modal, my body reverts back to overflow:auto allowing the page to scroll again. These functionaliti ...

"Enhance your webpage with Flexslider and captivating stretched images

When working with Flexslider, I encountered a challenge where the image was being stretched to fit the width of the flexslider. I prefer the image to be displayed centered, keeping its original dimensions, and having a black background-color. Is there a ...

Apache conf file configured with CSP not functioning properly when serving PHP files

While configuring the Apache CSP lockdown for a site, I encountered an unusual behavior when opening the same file as a PHP script compared to opening it as an HTML file. The HTML file looks like this: <html> <head> <meta http-equiv= ...

Tips on storing the amount of time a user devotes to answering questions on my form into a database

I am facing an issue with storing the "duration" in my database. The "duration" I aim to store is the time spent by the user answering a question I created. When they click the submit button, I want to save the duration they spent in the database. Below i ...

JavaScript accordions failing to open

I've encountered an issue with my website that includes JS accordions. Strangely, they are not opening on the live site, but they function properly on Codepen. I checked the console in Chrome and found no error messages, however, when I looked at the ...

What could be the reason for the padding not functioning properly on my Bootstrap 5.3 webpage?

What could be the reason for pe-5 not functioning properly? <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10727f7f64636462716050253e233e22">[email protect ...

Anomaly in Form Validation with Semantic-UI

This time around, I am implementing Semantic-UI with React. In the past, I have successfully utilized Semantic-UI's form and form validation features in various projects without encountering any issues. However, a new problem has arisen, which I will ...