an occurrence of an element being rendered invisible, within an instance of an element being rendered as flexible,

I am trying to create a button that will show a list of elements. Within this list, there is another button that can be used to either close the list or hide it entirely. However, for some reason, my code is not functioning properly.

let btn1 = document.querySelector('#btn1');
let btn2 = document.querySelector('#btn2');
let article = document.querySelector('article');


btn1.addEventListener('click', (e) => {
    article.style.display = 'flex';
})


btn2.addEventListener('click', (e) => {
    article.style.display = 'none';
})
div {
    position: relative;
    width: 100px;
    height: 100px;
    background-color: white;
}

article {
    position: absolute;
    left: 100px;
    display: none;
    flex-direction: column;
}
p {
    color: red;
}

span {
    position: absolute;
    right: -100px;
    color: white;
}
<div id="btn1">
        <article>
            <p>hello world</p>
            <p>hello world</p>
            <p>hello world</p>
            <span id="btn2">btn2</span>
        </article>
    </div>

This is important

Answer №1

Something interesting is happening in your situation. The nested divs that are listening to the click event cause the event to "bubble up" and reach the parent, resulting in both handlers being triggered for the same click. This leads to your element being hidden and then immediately shown again.

To prevent this from happening, I used event.stopPropagation() in the function that handles the click event for the child element.

https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

let btn1 = document.querySelector('#btn1');
let btn2 = document.querySelector('#btn2');
let article = document.querySelector('article');

btn1.addEventListener('click', (e) => {
    article.style.display = 'flex';
})

btn2.addEventListener('click', (e) => {
    article.style.display = 'none';
    event.stopPropagation();
})
div#btn1{
  border: solid 1px red;
  cursor: pointer;
}

div {
    position: relative;
    width: 100px;
    height: 100px;
    background-color: white;
    border: solid 1px black;
}

article {
    position: absolute;
    left: 10px;
    display: none;
    flex-direction: column;
    border: solid 1px black;
}
p {
    color: red;
}

span {
    position: absolute;
    cursor: pointer;
}
<div id="btn1">
    <article>
        <p>hello world</p>
        <p>hello world</p>
        <p>hello world</p>
        <span id="btn2">Click Here</span>
    </article>
</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

Setting the default dropdown option in Angular

For my latest question, I decided to utilize ng-init. However, upon loading the page, I noticed that there is a blank default option. When I click and select another option, the blank option disappears. How can I remove this initial blank option? Here is ...

What could be causing the background image on my Jumbotron to fail to load?

Having some trouble with setting my image as the background of the Jumbotron. I've tried a few different approaches, but none seem to be working. It's not an issue with static loading because using img src directly into the Jumbotron section does ...

Creating a List Item with Equal Height as Its Parent Container

<nav> <ul id="navUl"> <li> <div class="settingsDiv"> hey </div> </li> </ul> </nav> I am trying to adjust the height of the div element to match the height of the nav. My g ...

What is the process for transforming a string into a Cairo felt (field element)?

In order to work with Cairo, all data must be represented as a felt. Learn more here Is there a way to convert a string into a felt using JavaScript? ...

When you hover over the page, the content shifts to reveal a hidden div

Trying to figure out how to show additional content when a photo is hovered over without shifting the rest of the page's content placement? Looking for alternative solutions rather than using margin-top: -22%. Here's a link to the website in que ...

Results not showing up

After attempting to incorporate the scores into a table, I am facing an issue where they are not displaying. Can anyone offer assistance with this problem? I am currently hosting on GitHub Pages. File: https://raw.githubusercontent.com/Eternal-Network/Ete ...

Issue with scroll animation across multiple div elements

I am working on a project where I have two main divs, one with the id of "left-div" and the other with the id of "right-div". In the "left-div", there are multiple nested divs each with their own unique id. The overflow-y property of the "left-div" is set ...

The Tailwind style did not completely translate when applied to the content of dangerouslySetInnerHtml

I have an HTML file containing Tailwind styles stored in my database, which needs to be fetched first. This will result in an HTML string that will be inserted into dangerouslySetInnerHtml. Here is the code snippet (utilizing Qwik): export const useHTML = ...

Guide to configuring an Angular Material Footer using Flex-Layout

Could someone help me with setting up the footer in my Angular Material app? I want it to: stick to the bottom when the content height is smaller than the view-port move down or get pushed down when the content height exceeds the view-port One important ...

Creating a dynamic table layout with Bootstrap

Can someone help me figure out how to make this table responsive using Twitter Bootstrap 4? Here's the JS Fiddle Link for reference. <div class="some-table-section"> <div class="container"> <table class="table some-table"> ...

What could be the reason why the navbar ul li a instance is not appearing in Bootstrap 4 modal when using an

Can anyone help me solve the issue I'm having with viewing HTML in a Bootstrap 4 modal? Everything shows up except for the navbar ul li a elements. I've searched for a solution everywhere, but haven't found one yet. Please assist! (I want t ...

What is the method for applying a stroke-width:1 to specific sides of SVG shapes?

When setting a stroke-width: 1 on a <rect> element in SVG, typically a stroke is placed on every side of the rectangle. Is there a way to specify a stroke width on just three sides of an SVG rectangle? ...

Center text inside a d3 svg element

As a newcomer to the d3 library, I am facing challenges with a basic task. Inspired by this example on gradients, I have integrated a linear gradient into the footer div element: #footer { position: absolute; z-index: 10; bottom: 10 ...

Attempting to modify the element's value with the help of selenium

I am facing an issue while trying to change the element through Selenium. Despite using send keys and execute_script, the value remains unchanged. I am attempting to set the value to 'R'. driver.find_element_by_name('wlw-select_key:{actionF ...

AngularJS: splitting the parent <div> into multiple sections every nth element

I have an array of strings in Javascript and I am attempting to use AngularJS to create nested <div> elements. var arr = ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu"]; My goal is to group every 3 elements together like so. <div class="pare ...

Label the timeline map generated with the leaftime plug-in for leaflet in R with the appropriate tags

Here is a code snippet extracted from the R leaftime package documentation examples. It generates a map with a timeline that displays points as they appear over time. I am interested in adding labels to these points to show their unique id numbers. Upon ...

In HTML, discover a sneaky way to conceal the video progress bar

I'm having trouble using webkit and it's not working for me #videoP::-webkit-progress-value { display: none; } #videoP::-webkit-progress-bar { display: none; } This is the code I have for my video in HTML <video id="videoP&quo ...

Tips for replicating input boxes in a while loop

HTML : <table> <tr> <td> <input type="text" name="name[]" value=" echo $ail"> <label >Address</label> <input type="text" name="countryname[]" id='countryname_1 ...

What is the process of generating a VectorSource in OpenLayer 6.5 using a JavaScript object?

I'm in the process of developing a web application that utilizes OpenLayer 6.5. I need to dynamically mark certain locations without storing ".geojson" files on the server. Any suggestions on how I can achieve this? When attempting to create a Vector ...

What is the best way to obtain the output of the MULTIPLO.SUPERIOR function using JavaScript?

In Microsoft Excel, there is a function called upper multiple where we can input a number and it will round up to the nearest multiple of a second specified number - for example: 10,986 ; 5 = 15 105,32 ; 5 = 110 ...