Show a div element when clicking on a different div element

Three images have been added. Please take a look: https://i.sstatic.net/R4IgG.png

What would be the optimal approach to create a similar layout? My current idea involves creating a large container after each row, which is initially collapsed. Upon clicking on one of the three overlapping containers, the container would expand and display the corresponding text. However, I am concerned about what would happen if the display cannot accommodate all 3 divs in a row when using flex boxes. Is there a more efficient solution that requires minimal jquery?

Answer №1

One potential starting point could be a structure like the following: https://example.com/unique-link

HTML

<div class="container">
  <div class="section">
    <div class="box">
    </div>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </div>
  <div class="section">
    <div class="box">
    </div>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </div>
  <div class="section">
    <div class="box">
    </div>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </div>
</div>

Javascript

document.querySelectorAll('.section').forEach((box, index) => {
  box.style.order = index * 2;
});

document.querySelectorAll('.box').forEach(box => {
  box.addEventListener('click', event => {
    var newSection = document.createElement('div');
    newSection.classList.add('section');
    newSection.style.order = +event.currentTarget.parentNode.style.order + 1;
    var newBox = document.createElement('div');
    newBox.classList.add('box');
    newSection.appendChild(newBox);
    event.currentTarget.parentNode.parentNode.appendChild(newSection);
  });
});

CSS

.box {
  min-width: 100px;
  height: 50px;
  flex: 1;
  border: 1px solid black;
  flex-wrap: nowrap;
}

.section {
  width: 350px;
  display: flex;
  flex-direction: row;
}

.container {
  display: flex;
  flex-direction: column;
}

Answer №2

If you're looking to tackle this issue, using JavaScript is a viable solution. Take a look at the following code snippet...

<div id="a"> 
  <a href="#e" onclick="goB()"><h3>welcome</h3></a>

</div>
<div id="b">
  <a href="#er" onclick="goA"> <h3>hello...</h3> </a>
</div>

Now let's delve into the JavaScript code within an external file:

function goB()
{
  document.getElementById("a").style.display="none";
  document.getElementById("b").style.display="block";
}
function goA()
{
  document.getElementById("b").style.display="none";
  document.getElementById("a").style.display="block";
}

Answer №3

To implement a hidden div, first add a div element in the HTML and set its display property to hidden. Then, use the onclick event to change the display property to block for that particular div.

window.addEventListener("load", function(){
     document.getElementById("click").onclick = function(){
       document.getElementById("div-1").style.display = "block";
     };
});
.row
{
    width:100%;
}

.one-third
{  
  width:33.33333%;
  float:left;
  padding:30px 0;

}

.full-width
{
  display:none;
  width:100%;
  text-align:center;
}
<div class=wrap>
  <div class="row">
    <div id="click" class="one-third">
      Column 1-Click Me
      </div>
     <div class="one-third">
      Column 2  
    </div>
     <div class="one-third">
       Column 3
      </div>
    </div>
<div class="full-width" id="div-1">Full width Div</div>
 
  <div class="row">
    <div class="one-third">
      Column 1
      </div>
     <div class="one-third">
       Column 2
      </div>
     <div class="one-third">
       Column 3
      </div>
    </div>

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

What is the best method for sending form data, specifically uploaded images, in Python Bottle with Ajax?

This form belongs to me <form method='post' enctype='multipart/form-data' id='uploadForm' name='formn'> <input type='file' value='' name='newfile'> <input type=&a ...

Troubleshooting: Issue with displaying PHP data on an AngularJS table using JSON through jQuery AJAX

I've been encountering an issue while trying to display data from a database on my HTML page using Angular and JSON. Despite the browser being able to read the database, it fails to show the data. Can anyone shed some light on why this might be happen ...

The class .is-invalid transforms into .is-valid when rendered

Currently, I am incorporating bootstrap into my react project. In this case, I have a variable called mobile that needs to undergo validation whenever there is a change in the input field. Below is the code snippet for the component: const EnterMobile = ( ...

Prevent parent from scrolling when hovering over a div in HTML5

Unfortunately, the scrolling attribute is not supported in HTML5. I'm facing an issue where I have an svg inside an iframe within a div, and I need to enable scroll functionality within the div while preventing the page from scrolling at the same time ...

Running multiple npm scripts on both Unix and Windows systems can be achieved by using the "npm

Is there a way for me to execute multiple npm scripts synchronously, one after another? Below are the npm scripts I have in my project, which includes both bower and npm packages: { "scripts": { "installnpm": "npm i", "installbower": "bower i", ...

Error in Leaflet: Uncaught TypeError: layer.addEventParent is not a function in the promise

Having trouble with Leaflet clusterGroup, encountering the following error: Leaflet error Uncaught (in promise) TypeError: layer.addEventParent is not a function const markerClusters = new MarkerClusterGroup(); const clusters = []; const markers = []; co ...

Encountering an issue with retrieved items upon refreshing the webpage

My usual approach to fetching data from an external API involves the following steps: Using Fetch API: const [tshirts, setTshirts] = useState([]); const fetchData = () => { fetch('apiEndpoint') .then((response) => ...

Utilizing intricate CSS selectors for integration testing with webdriverjs

In my quest to develop integration tests using Selenium with JavaScript bindings, WebdriverJS, Mocha, and Chai, I stumbled across an interesting suggestion in this article. It proposed using WebdriverJS over the official SeleniumJS bindings for some reason ...

React's POST request is not received by Node

Here is the React code I am using to post: const [nickname, setNickname] = useState('') const [title, setTitle] = useState('') const [content, setContent] = useState('') const [agree, setAgree] = useState(false) const [thepost ...

Gruntjs Live Reload is actively monitoring for changes, yet fails to refresh the page

Check out my Gruntfile.js here Also, take a look at my package.json here I ran npm install in the main directory of my workspace (using WAMP) and it created a node_modules folder along with several subfolders. After navigating to the directory c:\w ...

In JavaScript, apply a red color style to an appended list item every third time it is added

It should be something like this: <ul id="list"> <li>1</li> <li>2</li> <li style="color: red;">3</li> <- Text should be red ... <li style="color: red;">6</li> <- red ...

"Upon loading the page, I encounter JavaScript errors related to Angular's ngOnInit function. However, despite these errors,

I have a page in angular where I am loading data in the ngOnInit function. The data loads correctly and is displayed on the page, everything seems to be working fine. However, I am encountering numerous javascript errors in the console stating cannot read ...

How can I fix the issue with Bootstrap4 input file upload not displaying the uploaded file name in the input field? Is there a way to

I am facing an issue in my Angular component where I have used a bootstrap 4 input file tag. After selecting a file, it does not display the selected file name in place of "choose file". Could someone please advise on how to show the selected file name in ...

Issue with MaterialUI value prop not updating after dynamic rendering of components when value state changes

As I dynamically generate Material UI form components, I encounter an issue with updating their values. The value prop is assigned to a useState values object, and when I update this object and the state, the value in the object changes correctly but the M ...

The position of the absolute item is not determined in relation to its container

I am experiencing an issue with two items in a 'relative' container. The problem arises when the 'absolute' item should be positioned relative to the container, but instead, it is being placed relative to the second 'static' ...

Vue3 - Managing the Enabling and Disabling of Text Input and Checkbox Based on Input Number

<div class="container-body" v-for="index in 10" :key="index"> <div class="container-content"></div> <div class="container-content"> <input :id="'Row&apo ...

Update the appearance of Morris Donut following an AJAX request

Currently, I am implementing Morris Donut for a dashboard project and using AJAX to fetch data with two date ranges as parameters. However, I am encountering an issue where entering new date ranges results in rendering a new Donut Chart on top of the exist ...

Using a personalized function in JQuery

I need to execute a JavaScript function I created after a JQuery event has been triggered. The function in question is called scrambleDot, and I defined it previously like so:var scrambleDot = new function() { //my code }. Here's the attempted implem ...

Design unique bar graphs to display on your website

I'm working on developing a webpage that will display dynamic data in a visually appealing bar graph. The design and aesthetics of the graph are crucial in this project. You can view the desired design of the bar graph by clicking on this link to the ...

Transform a Linear Gradient in CSS into a Stylish Bootstrap Button in HTML

I've crafted an American football field using only CSS. In my Codepen sandbox, you can find the code (I'm utilizing ReactJS). What I'm aiming to achieve is to convert each -webkit-linear-gradient into a clickable button that logs its positi ...