Launch a new pop-up window that will disappear once the user clicks outside of the window

In my Java web application, I'm attempting to create a pop-up window that appears over the parent window when a user clicks on a link. The pop-up window will display values fetched from a database using Hibernate, and the user can select a value. Once the user selects a value and clicks the "OK" button within the pop-up window or anywhere outside it or in the parent window, the pop-up will hide.

Answer №1

To ensure that your wrapper element covers your parent window but stays below the popup window, follow these steps:

First, create a wrapper element with a higher z-index than the parent window.

Add an event listener for "click" to this wrapper element.

If the click target matches the wrapper element, close the popup and delete the wrapper element itself.

This setup will manage clicks outside of the popup effectively.

Remember to handle other events within your window accordingly.

Additional Styles

        html {
            height: 100%;
        }

        body {
            position: relative;
            height: 100%;
            z-index: 1;
        }

        #overlay {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            background: rgba(0,0,0,0.25);
            z-index: 99;
        }

        #popup {
            position: absolute;
            width: 20%;
            height: 20%;
            top: 40%;
            left: 40%;
            background: rgb(220,220,220);
            box-shadow: 2px 2px 3px rgba(0,0,0,0.5);
            z-index: 100;
        }

HTML Code

<input id="popupbutton" type="button" value="pop me up" />

Javascript Logic

<script>
        document.getElementById('popupbutton').addEventListener('click', loadPopup, true);

        function loadPopup(e) {
            e.preventDefault();
            e.stopPropagation();

            var overlay = document.createElement('div');
                overlay.id = 'overlay';
                overlay.addEventListener('click', closePopup, true);

            var popup = document.createElement('div');
                popup.id = 'popup';

            document.body.appendChild(overlay);
            document.body.appendChild(popup);

            function closePopup(e) {
                e.preventDefault();
                e.stopPropagation();

                // Close everything only if clicked on overlay
                if (e.target.id === 'overlay') {
                    document.body.removeChild(popup);
                    document.body.removeChild(overlay);
                }
            }
        }
    </script>

Updated JS Fiddle Link http://jsfiddle.net/md063bfr/1/

Answer №2

One way to utilize a div element is as follows:

<td>
  <div align="center" id="show_more" style="background-color: blue;display:none;width:550px;height:250px;top:80px;overflow: auto;">
    Insert your content here
  </div>
</td>

Then, use a JavaScript function to toggle the display property from none to show:

function DisplayDiv(){
            Show.show('show_more');
        }

That's all for now. Thank you.

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

Make sure to remain in the same division area when refreshing the page

Whenever I click the next button and move to step 2, I want the page to stay on the same div even after reloading. The two divs used are join_form_1 and join_form_2 Currently, when I reload the page, it reverts back to the first div. Is there a way to e ...

Having trouble with KnockoutJS and its foreach functionality

Having trouble looping through an object using foreach in my code - the struggle is real after 6 months of not coding! This code snippet works perfectly: <div data-bind="text: $root[36].partition"></div> But I can't seem to get the fore ...

How can JavaScript/jQuery be used to update LocalStorage Objects when editing a form?

Having trouble pinpointing an issue with my code. Despite making modifications, the values in localStorage are not updating as expected. Any suggestions on what may be causing this problem? Note: Changing const idx to const i resulted in only the final va ...

On one webpage, IE 11 is correctly styling certain visited hyperlinks while failing to do so for others

Although I acknowledge that others have asked similar questions, none seem to address the exact issue I am facing, nor do they offer practical solutions or clear explanations. Issue I'm encountering a problem where some visited hyperlinks in IE 11 f ...

Open a submenu when clicking on an input field and automatically close it when the focus is lost

Is there an easier way to create a submenu that opens on input click and closes when losing focus using jQuery? Right now, I have achieved this functionality with the following code: $(document).mouseup(function (e){ var container = $(".container"); ...

Validation for a datepicker embedded within a table

I'm facing a challenge and need some assistance. I want to validate if the "FROM (DATE)" is greater than the "TO (DATE)" without disabling the date selection, but instead prompting the user if the condition is not met. Any insights or solutions would ...

"Commitment made ahead of time without allowing for the outcome to

I'm completely new to working with promises and I'm encountering some unexpected behavior in my code. The issue lies in the TaskRunner.SyncObjects function within Main.js, which should be waiting for the selectedCourses variable to be populated ...

Getting a hold of an Array value from an external scope in Angular JS

I've been struggling with this issue for a while and can't seem to find a solution. I have a code snippet where I'm trying to pass values from getJSON to an angular controller, but my array loses its values in the process. Can someone please ...

How to trigger an Angular JS route without loading a view

Could someone help me with calling the /auth/logout url to get redirected after a session is deleted? app.config(['$routeProvider',function($routeProvider) { $routeProvider .when('/auth/logout',{ controller:'AuthLo ...

JavaScript/CSS memory matching game

Just starting out in the world of programming and attempting to create a memory game. I've designed 5 unique flags using CSS that I want to use in my game, but I'm feeling a bit stuck with where to go next. I understand that I need some function ...

How does the functionality of $.ajax differ from that of $.get?

Similar Inquiry: Understanding the Variations of $.ajax(), $.get(), and $.load() I'm curious about the disparities between $.get() and $.ajax The given code showcases calls like this: $.get(href) .success(function (content) { $(&apos ...

Is there a way to retrieve the device id in a React JS application?

After using react-native-device-info in my mobile app, I am now looking for a way to retrieve the device ID in my web app using React js. Are there any NPM packages or alternative methods available for this? I have not come across any node package specific ...

Combing external JavaScript with React functionality

Hey there, I've been working on merging two projects that I came across recently: https://github.com/danxfisher/MeetEasier and this awesome page https://tympanus.net/Development/Interactive3DMallMap/ After making some changes in the MeetEasier React ...

Creating distinctive HTML identifiers with a PHP loop

Hi everyone, I'm a newcomer to form creation and I'm excited to hear all of your insights. Currently, I'm working on a small website and I've encountered an issue with generating unique HTML ids. I am using a loop in PHP to generate th ...

How to display a conditional component in a single line with Material UI

I'm facing an issue with a component that is being reused multiple times. Here's the current output: |MyComponent1| |MyComponent2| Specifically, my condition is: if (a == 'One' || b == 'Two'){ <MyComponent data={data}/> ...

Does Three.js lighting adjust according to the bundler used?

Today, I decided to streamline my portfolio project by transitioning it from standard HTML to the Vite bundler for easier dependency management. I simply copied and pasted the existing code, making adjustments to the imports since I had been using relative ...

Vue.JS has issued a warning indicating that the `util._extend` API is no longer supported. To address this, developers are advised to utilize Object

During the execution of a call API request, the Vue.JS application encountered an error. The API is hosted at Okta, and the request is successful when using cURL in the CLI. Error Message (node:82171) [DEP0060] DeprecationWarning: The `util._extend` API i ...

Changing the Color of Material-UI Slider: A Step-by-Step Guide

Looking to update the color of Material UI Slider component I have attempted to adjust the CSS styling without success, followed by trying the solution provided in this issue and applying the following code, but the changes are not being reflected: getMu ...

CSS in flexbox nested with no overflow utilized

I have created a basic "table" layout using flexbox. I want to implement horizontal scrolling for a specific part of the table by using overflow-x property. When you look at the provided code snippet, you'll notice that the CSS styles are only visib ...

Is it possible to retain the placeholder text if the user selects the input field and the value is left empty?

Is there a way to make the placeholder text disappear only when the user starts typing at least one character in the input field? Currently, the placeholder disappears as soon as the user focuses on the input. Any suggestions would be appreciated. Thank ...