Hide the popup menu when the user clicks outside of it

I am presenting the following code

<!DOCTYPE html>
<html>
<head>
    <title>GalacticCraft</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <link rel="shortcut icon" type="image/png" href="favicon.ico">
</head>
<body>
<div id="staff"></div>
    <div id="portal">
        <img id="logo" src="assets/logo.png">
        <div id="buttons">
            <a href="http://forums.galacticmc.net"><div class="button" id="button1"></div></a><a href="http://store.galacticmc.net"><div class="button" id="button2"></div></a><a href="http://galacticmc.net/rules"><div class="button" id="button3"></div></a><div class="button" id="button4" onclick="staff()"></div><a href="http://galacticmc.net/vote"><div class="button" id="button5"></div></a>
        </div>
    </div>
</body>
</html>
<script>
function staff() {
    document.getElementById('staff').innerHTML = `
<div class="popup" onclick="close()"></div><img src="assets/close.png" class="close-icon" onclick="close()"><div class="popup-menu"><a href="http://www.galacticmc.net/staff-application"><img src="assets/apply.png" class="apply"><p>Apply to become a member of staff</p></a><hr><a href="http://galacticmc.net/staff"><img src="assets/staff.png" class="list-staff"><p>View Staff Members</p></a></div>
    `
}

</script>

and CSS

.popup {
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    position: absolute;
    z-index: 5;
    opacity: 0.5
}
.popup-menu {
    top: 0;
    width: 700px;
    height: 99.6%;
    background-color: #fff;
    position: absolute;
    z-index: 10;
    left:calc(50% - 351px);
    border: 2px solid #808080;
        -webkit-animation: fadein 1s; /* Safari, Chrome and Opera > 12.1 */
       -moz-animation: fadein 1s; /* Firefox < 16 */
        -ms-animation: fadein 1s; /* Internet Explorer */
         -o-animation: fadein 1s; /* Opera < 12.1 */
            animation: fadein 1s;
    padding-left: 20px;
}
.apply {
    width: 350px;
    height: 350px;
    position: relative;
    left: calc(50% - 185px);
}
.popup-menu p {
    color: #009AFF;
    text-align: center;
    font-size: 35px;
}
.popup-menu a {
    text-decoration: none 
}
.list-staff {
    width: 350px;
    height: 350px;  
    position: relative;
    left: calc(50% - 185px);
}
.close {
    position: absolute;
    top: 2px;
    right: 2px;
}
html {
    background-image: url("assets/bg.png");
    background-size: 100%;
    overflow: hidden;
    width: 100%;
    height: 100%;
}
#portal {
    bottom: 0;
    height: 550px;
    left: 0;
    margin: auto;
    position: absolute;
    right: 0;
    text-align: center;
    top: 0;
    width: 1000px;
}
#buttons {
  bottom:75px;
  height:200px;
  position:absolute;
  width:1000px;
}

.button {
  background-position:center;
  background-repeat:no-repeat;
  background-size:190px;
  cursor:pointer;
  display:inline-block;
  height:200px;
  transition:background-size .2s ease;
  width:200px;
}

.button:hover {background-size:200px}
.button:active {background-size:180px}

#button1 {background-image:url("assets/forums.png")}

#button2 {background-image:url("assets/store.png")}

#butt

on3 {background-image:url("assets/rules.png")}

#button4 {background-image:url("assets/staff.png")}

#button5 {background-image:url("assets/vote.png")}

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Opera < 12.1 */
@-o-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

Study our JSFiddle for further details https://jsfiddle.net/vgjvzmp5/

Upon clicking "Click me", you will observe a popup window appearing. It is functioning as intended, but how can I implement a method to close it when the faded background or the close icon in the corner is clicked? This implementation should be done using pure Javascript.

Answer №1

Check out this easy solution below. No need to constantly insert new html elements upon click. Simply include them in your html file with display:none and utilize javascript for toggling visibility:

function display() {
    document.getElementById('modal').style.display = 'block';
}

function vanish() {
    document.getElementById('modal').style.display = 'none';
}

https://jsfiddle.net/abcd1234/7/

Answer №2

To prevent event propagation, make sure to utilize the stopPropagation() method.

Check out a preview on CodePen!

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

JavaScript enables users to store over 5 megabytes of data on their client devices

Is there a way to store more than 5mb in the client browser? I need this functionality across various browsers including Firefox, Chrome, Internet Explorer, Safari (iOS), and Windows Phone 8 Browser. Initially, localStorage seemed like a viable option as i ...

JavaScript barcode data input

I am working with 2 inputs - #barcode and #quantity. When using a barcode scanner, I can easily enter the quantity after scanning data by pressing the Tab key. Typically, the quantity is null, indicating one piece. Currently, my cursor is in the #quantity ...

Mobile application designed using HTML which allows for storing data offline without needing an

Looking to develop a mobile application using HTML5 that can function on both iOS devices (such as iPhones and iPads) and Android phones and tablets. The focus of the app will be on organizing grocery items, including various categories and images for eac ...

The Facebook like button seems to be missing from the webpage

Hi everyone, I'm having trouble getting the like button code to work. I used the wizard on Facebook but the button still isn't showing up, even with a simple HTML setup like this: <!DOCTYPE html> <html> <head> </head> < ...

Tips for managing submitted data to a server in express?

In the process of sending a post request from my index.js to app.js upon a click event, I have the following code snippet: var data = { name: "Sarah", age: "21" }; var xhr = new XMLHttpRequest(); xhr.open("POST", "/", true); xhr.setRequestHe ...

Make sure to fully load the HTML page before running the PHP code

Here is the code snippet I am currently working on: <div class="text-center"> <img src="img/ford_lodding.gif" alt="Loading GIF" style="width: auto; height: 500px;"> </div> <?php require 'php/loading.php';?> The cont ...

The donut chart in Chart.js is stuck in grayscale without any colorization

I just set up a donut chart using chart.js with the following code: <div id="chartContainer" style="height: 320px; width: 320px"> <canvas id="hoursFEContainer"></canvas> </div> To use chart.js, I downlo ...

External JavaScript files cannot be used with Angular 2

I am attempting to integrate the twitter-bootstrap-wizard JavaScript library into my Angular 2 project, but I keep encountering the following error: WEBPACK_MODULE_1_jquery(...).bootstrapWizard is not a function I have created a new Angular app using a ...

Is it possible to simultaneously center and display an iframe inline?

I have a YouTube video that I'm trying to center within a div. After adding display: block; to the CSS following advice from How to center an iframe horizontally?, it centers correctly, but now I want to display two images inline next to the iframe ...

Is there a way to prevent the text box from expanding beyond the small breakpoint?

Help! I'm trying to control the size of a Bootstrap 4 input text box. I need it to stop growing after the small breakpoint, so it doesn't expand to the full width of the screen. ...

The input value in the HTML form was altered momentarily before reverting back to its original state

Researching this topic was quite challenging, but keep reading to find out why. My objective is to detect any changes in a form field so that I can enable the "Save" button. While this seems easy enough, there's a catch. If the user reverts the input ...

How to pass arguments to a function within a preg_replace in PHP

Greetings! I currently have a situation similar to this: $content = preg_replace('#(\s*)\<pre(.*?)\>(.*?)\</pre\>(\s*)#sie', 'dostuff(\'\\3\', \'\\2 ...

in javascript, how can you access the value of a parent object within a nested object?

Within the material ui framework, I am utilizing the createMuiTheme function to create a theme. How can I access the values of the parent object within a nested object? (I am aware that I can declare global variables above the function); To better unders ...

Delay the rendering of the MUI DataGrid column until after the DataGrid is visible on the screen

Would it be feasible to asynchronously load a column of data in the MUI DataGrid after the table is displayed on the screen? Retrieving this additional data from the database is time-consuming, so I aim to have it appear once the table has fully loaded. T ...

What happens with JQuery if the JQuery IIFE does not get executed properly?

The jquery-3.3.1.js file contains the following code: ( function( global, factory ) { ... } ); Within this Immediately-Invoked Function Expression (IIFE), there is a line allowing for the use of the JQuery and $ variables to call the JQuery function: wi ...

The script from 'URL' was declined for execution due to its MIME type of 'text/html' which is non-executable, in addition to strict MIME type checking being enabled

I encountered an error stating "Refused to execute script from 'URL' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled." The code that triggered this error is shown below. <!DOCTYPE htm ...

"Optimizing navigation with dynamic link routing in AngularJS

I am working on a list of apartments displayed using ng-repeat. I need each apartment to be a clickable link that directs the user to view more details about that specific apartment. However, I am facing an issue where the links are not being repeated dyna ...

What causes the width of unrelated cells to change when the contents of colspan'd cells expand in IE7?

Before we delve into the details, I invite you to take a look at this fiddle using IE7. It's quite intricate and not something I want to repeat here. The main issue is that clicking on the red block should display additional information. The top row ...

Can you upload a file using the MaterialUI Upload Button in React and then send it to Firestorage?

Hi everyone! I recently implemented MaterialUI's Upload Button in my project, which you can find here: https://material-ui.com/components/buttons/ Below you will see the code snippet where I have integrated this button and now I am trying to upload a ...

What are some methods for saving HTML form data locally?

Creating an HTML form and seeking a solution for retaining user data even after closing and reopening the window/tab. Utilizing JavaScript cookies or HTML 5 local storage would require writing code for every input tag, which could be time-consuming especi ...