Generate various pop-up windows on the screen

As a beginner in jQuery, I am currently exploring how to create multiple popups within a window. My goal is to have each text trigger a different popup with unique content. Additionally, I would like the popups to close when clicked outside of the modal, rather than solely relying on pressing Esc. Any guidance or tips on achieving this would be greatly appreciated. Thank you in advance.

HTML:

<div id="main">
           <h1 class="button" id="applepie">Apple Pie</h1>
                <div class="modal-mask"></div>
                <div class="modal-popup">hello</div>

                <h3>Asian Noodles</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup"></div>


                <h3>Avocado Roll</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup"></div>


                <h3>Bruscetta</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup"></div>


                <h3>Bagels</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup"></div>


                <h3>Banana Pudding</h3>
                <div class="modal-mask"></div>
                <div class="modal-popup"></div>

     </div>

CSS:

.modal-mask{
    width:100%;
    height:100%;
    position: absolute;
    top:0px;
    z-index: 100;
    background-color:#000;
    opacity:0.4;
    display:none;
    }

.modal-popup{
     position:fixed;
     top: 50%;
     left: 50%;
     width: 5%;
     height: 5%;
     z-index: 101;
     background-color:#fff; 
     display:none;
     }

JS:

$(function(){
  $(".button").on("click", function(){

    $(".modal-mask").css("display", "block");
    $(".modal-popup").css("display", "block");

  $(document).on("keydown", function(event){
   if(event.keyCode === 27){
    $(".modal-mask").css("display", "");
       $(".modal-popup").css({
           "display": "",
           "width": "",
           "height": "",
           "top": "",
           "left": ""
       });
    }
  });
});

Answer №1

Code Example with Escaped Detection and Modal Popup for Header Click

Resolved

  • The first modification to your code involves wrapping each header and two modal divs in a content wrapper div
  • Upon clicking on the header, the parent object is identified and the second and third elements' display:block are toggled accordingly

HTML

<div id="main">
     <h1 class="button" id="applepie">Apple Pie</h1>

     <div id="Container1">
    <div class="modal-mask"></div>
    <div class="modal-popup">hello</div>
    </div>  
    <div id="Container2">
     <h3>Asian Noodles</h3>
    <div class="modal-mask"></div>
    <div class="modal-popup">hello</div>
    </div>
    <div id="Container3">
     <h3>Avocado Roll</h3>

    <div class="modal-mask"></div>
    <div class="modal-popup">hello</div>
    </div>
    <div id="Container4">
     <h3>Bruscetta</h3>

    <div class="modal-mask"></div>
    <div class="modal-popup">bye</div>
    </div>
 <div id="Container5">
        <h3>Bagels</h3>

    <div class="modal-mask"></div>
    <div class="modal-popup">eat</div>
  
    </div>
    <div id="Container6">
     <h3>Banana Pudding</h3>

    <div class="modal-mask"></div>
    <div class="modal-popup">hungry</div>
    </div>
    
</div>

JQUERY

$("h3").on("click", function () {

 //   $(".modal-mask").css("display", "block");
//    $(".modal-popup").css("display", "block");
 $(".modal-mask").css("display", "none");
  $(".modal-popup").css("display", "none");
$($(this).parents().children()[1]).toggle();       
$($(this).parents().children()[2]).toggle();
    $(document).keyup(function(e) {

  if (e.keyCode == 27) {
   $(".modal-mask").css("display", "none");
  $(".modal-popup").css("display", "none");
  }   // esc
});
  
});

//Click out-side pop-up that ie on the mask close
$(".modal-mask").on("click", function(){

      $(".modal-mask").css("display", "none");
  $(".modal-popup").css("display", "none");
});

CSS

.modal-mask {
    width:100%;
    height:100%;
    position: absolute;
    top:0px;
    z-index: 100;
    background-color:#000;
    opacity:0.4;
    display:none;
}
.modal-popup {
    position:fixed;
    top: 50%;
    left: 50%;
    width: 15%;
    height: 15%;
    z-index: 101;
    background-color:#fff;
    display:none;
}

h3{
    background: -webkit-linear-gradient(left, red , blue); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, red, blue); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, red, blue); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, red , blue); /* Standard syntax */
    
}
h3:hover{
    background: -webkit-linear-gradient(left, blue , red); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, blue,red ); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right,blue ,red ); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right,  blue,red ); /* Standard syntax */
    
}

Output

Answer №2

To capture all click events by monitoring the document, we can use event delegation to determine the exact element where the click originated. Here's an example:

$(document).on('click', '*', function(){
    //Alternatively, you can also use the 'keydown' event.

    //If the click event did not bubble up through the modal,
    //it means the click occurred outside the modal on something else:
    if(!$(this).parents().hasClass('modal-popup')){
        //Perform actions when the user clicks outside the modal
        //For example, close the modal
    }
})

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

Using jQuery to calculate mathematical operations in a form

I've been working on creating a form that calculates the total cost based on selected options. So far, I've managed to get it working for three options, but I'm stuck on how to calculate the subtotal by adding the variables "vpageprice" and ...

Is there a method in VBA to access elements generated by javascript code?

After spending several hours conducting thorough research on Google (including browsing StackOverflow), I've been trying to find a method that would allow me to target HTML elements generated by JavaScript in VBA. For instance, using ie.Document.getE ...

What could be the reason for the gtag event not showing up in Google Analytics?

Here is an example of my script: <html> <head> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxx-1"></script> <script> wi ...

Variations in the appearance of scrollbars on the x-axis and y-axis within a single <div> element

Let's say we have a div element like this: <div class="nav-menu"></div> I want to customize the CSS style for scrollbar using the ::scrollbar pseudo-element: .nav-menu::-webkit-scrollbar { width: 8px; background: white; ...

Error: An identifier was unexpectedly encountered while using the Submit Handler

I am currently working on creating a validation script and an AJAX call. I have encountered a problem where the alert message is not working within the if condition. I can't seem to figure out what's causing this issue. When I execute the scri ...

Embed watermark on the Username and Password fields of asp:Login control

Seeking help to watermark the Username and Password fields of asp:Login controls using jQuery, I have attempted different methods to reference the control ID: $('#<%=ClientID.Login1_UserName %>').watermark('watermark', 'User ...

Step-by-step guide on navigating back in Angular 12 without triggering a page refresh

As an illustration, consider a scenario where I input values into a table and move to the next page. Upon returning to the page, I expect the entered values to still be present. I attempted to achieve this using this._location.back(), but instead of disp ...

What's the reason for the font weight property in CSS not affecting the font family?

The code structure in my HTML file includes a link to Google Fonts with the following call: <link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet"> Following that, I specify the font family and weight in the CSS section: ...

Can we implement a variety of site designs based on the size of the window?

Utilizing media queries allows us to modify the CSS for various screen sizes. My inquiry is: can we switch to a completely different page layout (or View in MVC) when the window size, specifically when the width is less than 500? Assuming that the control ...

What is the best way to place text beneath an input field?

Although it may seem simple, I am new to this and could use some help. I have an input box: <input type="text" name="username" id="username" class="form-control" value="{{username}}"> <div class="error" id="nameErr"></div> I have a func ...

Dealing with ASP.NET forms that involve a javascript plugin generating substitute input

I'm facing an issue with my ASP.NET MVC webpage where I submit a form using AJAX in the following way: function ValidateFormAndAjaxSubmit(formId, callingElement) { if (IsNotDblClick(callingElement.id)) { var _form = $("#" + formId); ...

A picture placed side by side with a list

Could someone please help me figure out what I'm doing incorrectly? <div class="row"> <ul class='list-group .col-md-6'> <li>1</li><li>1</li><li>1</li> </ul> <div> ...

Unlock the ability to target and manipulate the child element of a div using jQuery

function generateImage(num) { $("<div></div>") .addClass("imageContainer") .append($("<img/>") .attr("src", images[num].url) ).appendTo("#gallery"); }//End of function GenerateImage //--- ...

Television Mount for Precise Video Placement

Seeking assistance for a unique positioning challenge I am facing. I have a Video that needs to be placed on the home page with a TV-like image "frame" around it, and they should scale together. https://i.sstatic.net/2pLhi.png What I've attempted i ...

Having trouble transferring the information from my form to the datatables request

My code attempts to extract data from 3 inputs in a form control, however, upon clicking Submit, no data is captured and the page reloads. The submit triggers table.draw() as expected but fails to capture or add any values from the inputs to the Request ob ...

Triggering a JQuery event to switch between tabs

Currently, I am in the process of developing a web application that incorporates nav-tabs from Bootstrap. These nav-tabs consist of 4 different tab-contents. My main concern lies in determining which nav-tab the user has selected among the 4 tabs and updat ...

Modifying the dimensions of a text input box within an HTML string using React

I'm currently utilizing a popup library called sweetalert2-react-content to generate a popup box with a textbox and a text area. Even though the elements are being created successfully, I am struggling to adjust the size of the text area in the popupb ...

Using ng-repeat can cause conflicts with jQuery function calls

I implemented a combination of AngularJS and MaterializeCSS to display images using ng-repeat. MaterializeCSS comes with a jQuery-based materiabox function that triggers an animation to open a modal for each element with the materialbox class. However, I ...

Turn off automatic scrolling on Safari iOS 16 and 15 with single tab functionality enabled

Hey there! I'm encountering a frustrating issue that's really boggling my mind. When using iOS 15 or 16 with Safari and the "Single tab" setting enabled (which places the Safari search bar at the top), I noticed a strange padding animation when ...

What is the process for determining the overall cost of a column in Angular 9?

I am facing a challenge in calculating the total price, total quantity, and total counted(#) of each column in a table. | Item | Price | Quantity | 1 | Mouse | 500 | 1 | 2 | Wire | 100 | 2 | TI:3 | |TP:600 | TQ:3 | < ...