Jquery refuses to conceal a particular element

I've encountered this issue before, but I'm facing a problem this time. I'm attempting to create a popup that is initially hidden. When clicked, the popup does appear, but I'm unable to close it. Additionally, when trying to change the styles using developer tools from display:none to display:block, it automatically switches back. Currently, the popup is located in the head section of the page, but I have also tried placing it at the very bottom.

Here is the HTML code for the popup:

<div class="tablecell midlineunit popup-hml middle">
    <div class="hml-popup-bg popupbg ">
        <div class="hml-popup-cnt">
            <div class="closepopup">X</div>
        </div>
    </div>
</div>

And here is the JavaScript associated with it:

$(".popup-hml").each(function(){
     $(this).click(function(){
         $(this).find(".hml-popup-bg").show();
     });
});
$(".closepopup").each(function(){
    $(this).click(function(){
        $(this).parent().parent().hide();
    });
});

Lastly, .hml-popup-bg has been styled as hidden in the CSS.

Answer ā„–1

Here's a more efficient way to achieve the desired functionality: Instead of using .each(), you can simply utilize .click() and employ .closest() in place of parent().parent();

$(".popup-hml").click(function(){
    $(this).find(".hml-popup-bg").show();
});

$(".closepopup").click(function(e){
    e.stopPropagation();
    $(this).closest('.hml-popup-bg').hide();
});

Answer ā„–2

It seems like this solution should do the trick. By closing it without stopping propagation, it allows for re-opening through bubbling.

$(".popup-hml").each(function(){
     $(this).click(function(){
         $(this).find(".hml-popup-bg").show();
     });
});
$(".closepopup").each(function(){
    $(this).click(function(e){
        e.stopPropagation();
        $(this).parent().parent().hide();
    });
});

Check out the code snippet here

Answer ā„–3

It appears that your use of jQuery's .each() method is not in the correct place. Consider setting up 2 event listeners instead - one to open a popup when a button is clicked, and another to close it when the "X" is clicked.

You could achieve this functionality with the following jQuery code:

$(document).on("click", ".popup-btn", function(){
    $(".popup-overlay").show();
});
$(document).on("click", ".close-popup", function(){
    $('.popup-overlay').hide();
});

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

Quickly view products in Opencart will automatically close after adding them to the cart and redirecting the

I've integrated the product quickview feature into my OpenCart theme, which opens a product in a popup. However, when I add a product to the cart from the popup, it doesn't update on the main page until I refresh. I'm looking for a way to re ...

Can someone explain the inner workings of the Typescript property decorator?

I was recently exploring Typescript property decorators, and I encountered some unexpected behavior in the following code: function dec(hasRole: boolean) { return function (target: any, propertyName: string) { let val = target[propertyName]; ...

The functionality of classes containing <ul> elements is ineffective

Having recently embarked on the journey of learning html/css, I've encountered some confusion along the way. Allow me to showcase my basic web-page layout: <html> <head> <meta charset="utf-8"> <link rel = "stylesheet" type="text ...

Sometimes, Express may return the message "not found" on and off

After working with express for many years, I find myself a bit out of practice with TypeScript - and it seems like my eyesight is failing me! This is the first time I've encountered this issue, so I must be missing something... My current dilemma is ...

Tips for identifying when a tab has been reopened following closure?

There seems to be a problem with the JS state where it changes but then reverts back to its original state when the tab is closed and restored using cmd/ctrl + shift + t. Typically, data is fetched from the server via ajax using Vue's mounted() lifec ...

Using PHP and jQuery to fetch data from two different tables simultaneously

This code is set up to extract data from a single table exclusively. How do I modify it so it can retrieve data from two different tables and then store that information in another table? My goal is to achieve something similar to this: https://i.stack.i ...

Dynamic resizing navigation with JQUERY

I have successfully created a navigation menu that dynamically resizes each list item to fit the entire width of the menu using JavaScript. $(function() { changeWidth(500); function changeWidth(menuWidth){ var menuItems = $('#menu l ...

Iā€™m having trouble getting my home.html page to render in Spring Boot

This image perfectly encapsulates the essence of my code and project structure. I am encountering an issue where I cannot return the home.html file located in the static folder. I have tried specifying it as 'home' or 'home.html', but n ...

Is it possible to show a pop-up window containing aggregated data when the jQuery double-click event

How can I create a pop-up window to display aggregated data when the Double-click event is triggered in jQuery? In my code, each questionId has multiple related reasons. When a user clicks or selects a questionId button/event, the selected questionId will ...

ajax receives an empty responseText after sending intermediate data to Python on the backend

Initially, the frontend passed an identification to the backend. The backend utilized this identification to retrieve data from the database. The extracted data then underwent additional processing on the backend before being sent back to the frontend. Be ...

Using CSS and Vue, you can customize the appearance of inactive thumbnails by displaying them

My goal is for a clicked thumbnail to display in color while the others show as grey. The active thumbnail will be in color, and I want inactive thumbnails to appear in grey. Here is what I am trying to achieve: Vue.component('carousel', { ...

Is there a function for displaying the message "User is currently typing"?

I've been working on a chat system in PHP/jQuery, where the message 'User is typing a message...' appears at the bottom. Despite trying numerous methods, I have yet to succeed. My chat system utilizes PHP, MySQL, Ajax, and jQuery. While I&a ...

Executing Cross-Origin Resource Sharing in Angular and Node

I have implemented a frontend using Node.js and AngularJS, with the backend running on Jetty exposing REST services. I need to send a REST request from the frontend (Angular) to the backend. When I pass the complete URL to $http.get it works fine. However, ...

The content in the flex box item with horizontal scroll is overflowing beyond its boundaries

I am working with a flex box container that contains two child elements: a left side and a right side. I want the right side item to have a horizontal scrollbar in order to fit its content. .container { display: flex; overflow: hidden; height: 300 ...

Enhancing safety by utilizing varying programming languages

Is there a way to verify the login status using various programming languages? Currently, I am utilizing three sessions with the same name that begin simultaneously after the login process, incorporating ajax. At present, the processing of the login.html ...

New messages are revealed as the chat box scrolls down

Whenever a user opens the chatbox or types a message, I want the scroll bar to automatically move down to show the most recent messages. I came across a solution that seems like it will do the trick: The issue is that despite implementing the provided cod ...

Task: Choose MySQL option and Retrieve JSON data

I have a question regarding implementing a function in a separate module file and calling it within a route to retrieve query data: function getSobre() { return new Promise((resolve, reject) => { db.query(`SELECT * FROM sobre ORDER BY cod ...

display a new feature immediately upon the user's login

I encountered a scenario where the user is supposed to log in, and upon successful login, another component should be displayed. However, this functionality is not working as expected for me. I have to click the login button again or refresh the page to vi ...

Issue observed in Angular Material: mat-input does not show background color when in focus mode

https://i.stack.imgur.com/eSODL.png Looking for a solution regarding the mat-input field <mat-form-field> <input class='formulaInput' matInput [(ngModel)]='mathFormulaInput'> </mat-form-field> The blue backg ...

The issue of Angular curly braces malfunctioning in some simple code examples reversed my

<head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"> </script> </head> <body style="padding: 20px 20pc;"> <div ng-app="app"> ...