"Revamp the appearance of the div element upon clicking elsewhere on the

function replace( hide, show ) {
                document.getElementById(hide).style.display="none";
                document.getElementById(show).style.display="flex";
            }
@import url('https://fonts.googleapis.com/css2?family=Allerta+Stencil&family=Bebas+Neue&family=Inter:wght@100;200;300;400;600;700;800&family=Roboto:wght@300,400,700&display=swap');


    body {
        margin: 0;
        padding: 0;
    }

    .all_guidebook {
        width: 100%;
        position: absolute;
        min-height: 1000px;
        height: auto;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .title_guidebook {
        position: relative;
        top: 50px;
        justify-content: center;
    }

    .disposition_guidebook {
        width: 1100px;
        display: flex;
        position: relative;
        align-items: flex-start;
        top: 55px;
    }

    .navigation {
        width: 286px;
        background: var(--bleu);
        position: relative;
        height: auto;
        padding-bottom: 30px;
        border-radius: 35px;
        margin-right: 15px;
    }

    .navigation .all_links {
        position: relative;
        top: 15px;
    }

    .navigation h1 {
        color: white;
        font: 45px 'Bebas Neue';
        margin: 0;
        text-align: center;
        position: relative;
        top: 15px;
    }

    .navigation h2 {
        color: white;
        font: 34px 'Bebas Neue';
        position: relative;
        background: var(--fushia);
        display: flex;
        border-radius: 15px;
        height: 45px;
        padding-top: 2px;
        margin-bottom: 26px !important;
        width: 247px;
        box-sizing: border-box;
        top: 20px;
        margin: auto;
        justify-content: center;
    }

    .navigation .menu_deroul, #regl {
    display: flex;
    background: blue;
    width: 300px;
    }
    #reglhover {
    display: flex;
    background: blue;
    width: 300px;
    }
#reglhover img, #staffcredshover img, #ctxhover img, #grphover img, #pvrhover img, #syst1hover img, #syst2hover img, #pihover img, #exphover img, #foirehover img {
    transform: rotate(135deg);
    top: 5px;
    position: relative;
    left: 7px;
}
.navigation .menu_deroul img {
    float: left;
    left: 7px;
    position: relative;
    cursor: pointer;
}

.navigation span {
    color: var(--fushiapp);
    margin-right: 3px;
    font: 20px 'Bebas Neue';
}

.navigation h3 {
    color: var(--white);
    font: 20px 'Bebas Neue';
}
.wrap_deroul {
    display: flex;
    flex-direction: column;
    position: relative;
    top: -18px;
    align-items: flex-end;
}
.ancres_deroul {
    list-style: none;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    height: 30px;
    padding: 0;
    color: white;
    top: -16px;
    font-weight: 200!important;
    right: 16px;
    font: 16px 'Bebas Neue';
    position: relative;
}
.ancres_deroul ul {
    margin: 0;
    display: flex;
    cursor: pointer;
    list-style: none;
    align-items: flex-end;
    flex-direction: column;
}
.ancres_deroul li {transition: .5s;}
.ancres_deroul li a {
    text-decoration: none!important;
    color: black;
}
.ancres_deroul li:hover {
    letter-spacing: .5px;
    transition: .5s;
}
<div class="menu_deroul" id="regl" style="display:flex" >
                    <img src="https://i.ibb.co/cvhdX78/image.png" height="21" width="21" onclick="replace('regl','reglhover')" />
                    <div class="titre_deroul">
                        <span>001.</span>
                        <h3 class="tablinks" onclick="openCity(event, 'reglement')" id="defaultOpen">Règlement</h3>
                    </div>
                </div>
                <div id="reglhover" style="display:none">
                    <img src="https://i.ibb.co/cvhdX78/image.png" height="21" width="21" style="cursor:pointer; transform: rotate(135deg); transition: .5s; transition-duration: 2s;" onclick="replace('reglhover','regl')" />
                    <div class="wrap_deroul">
                        <div class="titre_deroul">
                            <span>001.</span>
                            <h3 class="tablinks" onclick="openCity(event, 'reglement')">Règlement</h3>
                        </div>
                        <div class="ancres_deroul">
                            <ul>
                                <li><a onclick="scrollWin()">Inscription & RP</a></li>
                                <li style="margin-top: -5px;"><a href="reglement.html#discord" target="myIframe">Discord</a></li>
                            </ul>
                        </div>
                    </div>
                </div>

I need help modifying this code to close the displayed div when clicking outside of it. The goal is to toggle between two div elements based on button clicks, but I want the currently shown div to be hidden when clicking anywhere else. Can someone assist me in achieving this functionality for all instances (since tabs are used)? Thank you.

Answer №1

test

const element = document.getElementById("element")
const elementhover = document.getElementById("elementhover")

document.addEventListener("click", (event) => {
    if (elementhover.style.display === "flex" && event.target.id != "elementhover") {
        elementhover.style.display = "none"
        element.style.display = "flex"

    }
})

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

Making AngularJS work with angular-ui bootstrap and ensuring compatibility with IE8

Having trouble getting AngularJS code that utilizes angular-ui bootstrap to function properly in IE 8? Following the guidelines in the AngularJS developer guide on IE didn't seem to solve the issue for me. I inserted the code snippet below into my ind ...

Having difficulty launching the sapper app in production mode

Having trouble starting my sapper project in production mode. When running npm run start, the following output appears on the console: niklas@Niklass-iMac project-name % npm run start > [email protected] start /Users/niklas/path/to/project > node _ ...

Load CSS styles once the HTML content has been generated by the PHP script

One way to dynamically add a section of HTML during page load is by making use of PHP in the index.php file: In your index.php file, you can include a section like this: <html> <head> <link href="css/style.css" rel="stylesheet"> ...

Encountering a problem with Axios get request error in React and Redux when communicating with a Laravel 5.2 API

Currently, I am utilizing react alongside redux and axios for handling asynchronous actions. The backend is powered by Laravel 5.2 API which is located on a subdomain, while React resides on the main domain. However, whenever I attempt to make an async GET ...

next-redux-wrapper is being invoked repeatedly and experiencing multiple calls to HYDRATE

I'm embarking on a new Next.js project, transitioning from a standard React app to a Next.js application. Our plan is to utilize Redux Toolkit for global state management and incorporate server-side rendering. During this process, we discovered the ne ...

Is there a way to integrate a calendar feature within an Angular 2 application?

I am brand new to Angular 2 and have a question: I need to integrate a calendar into a page where users can add events. Something similar to the following example: I'm unsure if the angular material calendar project is designed for Angular 2 or for ...

Removing a row from a table in a React component

I have incorporated a Table component from Material UI into my project to display data fetched from an external API. The table dynamically updates its rows based on events received through a web socket connection. One specific requirement I have is that wh ...

Translate a jQuery ajax request utilizing jQuery().serialize into plain JavaScript

Currently, I've been in the process of converting a jQuery script into vanilla JavaScript to completely eliminate the need for jQuery. The main functionality of the code includes: Upon clicking a button on the front end, an ajax request is sent, upda ...

Extract the text enclosed by two specific symbols within a string and add it to a new array

I have a string formatted as follows: var str = "-#A This text belongs to A. Dummy Text of A. -#B This text belongs to B. Dummy Text of B. -#C This text belongs to C. Dummy text of C. -#Garbage This string should be ignored" I am looking to convert this ...

Angular 2: Harnessing the power of Observables with multiple Events or Event Handlers

In the component template, I have grouped multiple Inputs and their events like this: <tr (input)="onSearchObjectChange($event)"> <th><input [(ngModel)]="searchObject.prop1"></th> <th><input [(ngModel)]="searchObje ...

Modify the transition and design of two progress bars

I'm having an issue with merging two Bootstrap progress bars into one. Initially, the first progress bar appears when the page loads and hides after data is loaded. At the same time, the second progress bar shows up. However, the transition animation ...

How to simultaneously play a sound and display an alert message using JavaScript

My JavaScript code attempts to play a sound and then display an alert, but the alert always shows up first. When I click 'ok', the sound plays. How can I make it so that the sound plays before the alert appears? <script> function play() { ...

Obtain an indeterminate value from a variable

My situation involves a dynamic variable assigned from a service, requiring a real-time calculator to update another variable using its value. Below is the relevant code snippet: $scope.getSubTotalSCTax = function(){ TableService.checkOut('SubTo ...

Issue with undefined bindingContext.$data in IE9 on knockout binding handler

I'm attempting to create a unique binding handler that applies role-based access to fields on a page. This custom handler uses the values of other observables from the viewModel to enable or disable input controls based on certain conditions. However ...

Exploring the depths of JavaScript JSON elements

After processing my PHP code, it generates a JSON output that contains multiple entries in the same structure. Here is an example with two entries: { "0": { "campaign_id": "31", "title": "new title", "description": "new descrip ...

The triggering of routing in Next.js is not established by useEffect

I'm facing an issue with my Next.js dynamic page that uses routing based on steps in the state. The route is supposed to change whenever a step value changes, like from null to "next" or back. However, the useEffect hook doesn't seem to be reacti ...

Strip the CSS styling from an image (the CSS being included in the generated code)

I need to remove the CSS styling from an image, but I can't modify the generated code where it's coming from. My actual code looks like this: <div class="bubble"> <img id="one" src="/static/webupload/MyronArtifacts/images/descarga.png ...

Organize intricate JavaScript Arrays

Similar Question: How to sort an array of javascript objects? I'm in the process of transitioning some of my older ActionScript 2.0 code to JavaScript. While most things are running smoothly, I've hit a roadblock when trying to numerically s ...

Guide on efficiently inserting values into an array of objects

I'm looking to extract specific values from the enum below enum p { XDR = 1, PT1M = 2, PT1M_ = 2, PT5M = 3, PT5M_ = 3, PT15M = 4, PT15M_ = 4, PT1H = 5, PT1H_ = 5, P1D = 6, P1D_ = 6, P7D = 7, P1W = 7, ...

What is the best way to monitor changes in objects within a JavaScript array?

Currently, I am in the process of developing a Todo application using electron and Vue.js. In my project, there is an array of objects called items. Each object follows this format: {id: <Number>, item: <String>, complete: <Boolean>, ...