CSS and HTML modal not closing

I've been working on creating a custom popup using HTML, CSS, and some jQuery functions. My idea was that when I click on the main div, it should expand in size and display a cancel button, which it does successfully. However, the issue arises when trying to close the popup by clicking the remove button.

Below is the HTML code:

<div class="custom-popup-div" id="popup">
    <div class="custom-popup-remove-button" id="popup-button">
        <span class="glyphicon glyphicon-remove"></span>
    </div>
</div>

And here are the scripts I'm using:

$(function () {
        $(document).ready(function () {
            $('.custom-popup-div').click(function () {
                $('#popup').removeClass('custom-popup-div');
                $('#popup').addClass('custom-popup-div-expanded');
                $('.custom-popup-remove-button').css("visibility", "visible");
            })
        })
    })

$(function () {
        $('.custom-popup-remove-button').click(function () {
            console.log("Cancel clicked !");
            $('#popup').removeClass('custom-popup-div-expanded');
            $('#popup').addClass('custom-popup-div');
            $('.custom-popup-remove-button').css("visibility", "hidden");
        })
    })

As for the CSS styling:

.custom-popup-div{
     position: fixed;
     bottom: 0;
     right: 0;
     margin-right: 15px;
     margin-bottom: 15px;
     width: 250px;
     height: 150px;
     background-color: yellow;
     border: 1px solid #000000;
     border-radius: 7.5px 7.5px 7.5px 7.5px;
}

.custom-popup-div-expanded{
     position: fixed;
     bottom: 10%;
     top: 10%;
     width: 80%;
     left: 10%;
     right: 10%;
     height: 75%;
     background-color: yellow;
     border: 1px solid #000000;
     border-radius: 7.5px 7.5px 7.5px 7.5px;
 }

.custom-popup-remove-button{
     position: relative;
     top: 2.5%;
     left: 97.5%;
     visibility: hidden;
 }

Edit: I forgot to mention that I also want the popup div to revert back to its original size. Thank you for your help!

Sincerely, Marcus

Answer №1

It's possible that when you click the button, it triggers another click event handler (attached to '.google-maps-div'), causing the popup to close and reopen immediately.

To prevent this behavior, try using e.stopPropagation(); like this:

$(function () {
        $('.google-maps-remove-button').click(function (e) {
            console.log("Cancel clicked !");
            $('#popup').addClass('google-maps-div');
            $('#popup').removeClass('google-maps-div-popup');
            $('.google-maps-remove-button').css("visibility", "hidden");
            e.stopPropagation();
        })
    })

Check out this DEMO

Answer №2

Modifying the styling of the button directly

$('.google-maps-remove-button').css("visibility", "hidden");

To conceal the popup, utilize the following code snippet

$('#popup').hide();

Answer №3

Implement CSS changes and toggle classes with a delay using the following code snippet.

$('.google-maps-remove-button').click(function () {
        setTimeout(function () {
            $('#popup').removeClass('google-maps-div-popup');
            $('#popup').addClass('google-maps-div');
            $('.google-maps-remove-button').css("visibility", "hidden");
        }, 100);           

    })

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

The mystery of the Accordion Effect: A Next.js/React.js issue where it slides up but refuses to

After implementing a custom accordion using next.js, I encountered an issue where the slide animation worked successfully when moving up and out upon clicking the button. However, when trying to move it back down into the content, it did not animate as exp ...

Having trouble with CSS text not wrapping correctly?

While working on a web application, I encountered an issue where the text within a <div> element on my page was not wrapping properly and instead overflowing outside the box. The <div> in question goes through two stages: one where it is not e ...

What is the best way to target and focus on all class fields that have a blank value?

<input type ="text" class="searchskill" value=""> <input type ="text" class="searchskill" value="3"> <input type ="text" class="searchskill" value=""> <input type ="text" class="searchskill" value="4"> Is there a way to target o ...

Looking for guidance on sending data from a JS file to HTML using Nodejs? Seeking advice on various modules to achieve this task effectively? Let's

Looking for advice on the most effective method to transfer data from a JS file (retrieved from an sqlite db) to an HTML file in order to showcase it in a searchable table. My platform of choice is NodeJS. As a beginner, I am willing to put in extra time a ...

Capable of retrieving the identification number but incapable of executing a POST request

After successfully retrieving the ID using the router, I encountered an issue when trying to confirm the change of agent status on the retireagent HTML page by clicking the submit button. The error message displayed is "ID is not defined". I am seeking ass ...

Assigning the style of the parent element based on the child element

Currently, I am attempting to develop a customized dropdown field and here is the code I have come up with: const list = document.querySelector('.list'); const listItems = list.getElementsByTagName('p'); document.querySelector('# ...

Traversing a two-dimensional array backwards in JavaScript

I am working with an array that contains different teams: The structure looks like this: leagues = new Array( Array('Juventus'), Array('Milan'), Array('Inter')); My goal is to iterate through the array and generat ...

Implementing scrolling functionality within a nested container

I need help with getting a nested container (.widget2) to scroll while still keeping the borders intact. Currently, the inner container is scrolling past the bottom edge of the parent container, and the scrollbar isn't displaying correctly. If you w ...

Implementing dynamic CSS switching for right-to-left (RTL) support in a multilingual Next.js application

As I work on my multilanguage application with Next.js, I'm encountering a challenge in dynamically importing the RTL CSS file for Arabic language support. My aim is to seamlessly switch between RTL and LTR layouts based on the selected language. M ...

Is there a way to ensure a dialog box is positioned in the center of the window?

After adjusting the dimensions of my jQuery UI dialog box with the following code: height: $(window).height(), width: $(window).width(), I noticed that it is no longer centered on the window. Do you have any suggestions on how I can recenter it? ...

Learn how to display specific text corresponding to a selected letter from the alphabet using Vanilla JavaScript

I am extracting text from a JSON file. My goal is to enable users to click on a specific letter and have the corresponding text displayed. For example, if someone clicks on 'A', I want to show the text associated with 'A'. An example of ...

Ways to align a nested list vertically

There are two nested lists: <ul> <li>First item <ul> <li> <a href="#">some item</a> </li> <li> <a href="#">some item</a> <</li& ...

Unable to execute AJAX POST request

https://i.stack.imgur.com/JqG7c.pngI have a JSON dataset like the one below: [ { "Password": "tedd", "Username": "john", "status": true } ] I need to consume this data using a POST method <label for="Username">Username:</label& ...

Using a nested table will override the "table-layout: fixed" property

I'm currently working on creating a tabular layout and I'm in need of the following: 2 columns with variable widths Columns that are equal in width Columns that are only as wide as necessary After some research, I discovered that by setting "t ...

``To ensure Google Maps fills the entire height of its parent div, set the

Is there a way to display a Google Map at 100% of the parent div's height? I've noticed that setting the height to 100% makes the map not visible, but if I use a pixel value for the height, the map displays correctly. Are there any tricks or solu ...

css code for styling html elements

HTML: <link rel="stylesheet" type="text/css" href="styles.css"><table border="0" cellpadding="0" cellspacing="0" class="month"> <tr><th colspan="7" class="month">January 2017</th></tr> <tr><th class="mon">Mo ...

Count divisions using PHP and the LI function

I'm struggling with a loop that is responsible for displaying <li> elements, and I need to incorporate a new class into the first item, as well as every sixth subsequent <li> element. For example: while ($db_field = mysql_fetch_assoc($re ...

Duplicate text content from a mirrored textarea and save to clipboard

I came across some code snippets here that are perfect for a tool I'm currently developing. The codes help in copying the value of the previous textarea to the clipboard, but it doesn't work as expected when dealing with cloned textareas. Any sug ...

Tips for enabling only a single div to expand when the "read more" button is clicked

I'm having trouble with my blog-style page where I want only one "read more" link to expand while collapsing the others. I've tried various jQuery methods and HTML structures, but haven't been able to achieve the desired result. Can someone ...

Troubles with basic jQuery hide and show functionalities

Hey there! I've been working on a project for the past couple of days and I'm having trouble with creating a slider effect using jQuery hide and show. It's strange because my code works perfectly fine with jquery-1.6.2.min.js, but when I swi ...