Creating a dynamic popup that is triggered by clicking on a specific row within a table

I'm currently struggling with positioning a popup in the center of the screen and making it responsive. The code I'm using involves generating a popup for each table row, but I haven't been successful in achieving the desired result.

Edit: JSFiddle

Here is the HTML snippet:

<table>
   <tbody>
            <tr>
                <th>No.</th>
                <th>Categories</th>
                <th>Sub-Categories</th>
                <th>Counts</th>
                <th>description</th>
            </tr>
            <tr class="popupOpen" data-href="#entry1">
                <td>1</td>
                <td>Category 1</td>
                <td>Sub-Category 1</td>
                <td>12</td>
                <td>This is a test</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Category 2</td>
                <td>Sub-Category 2</td>
                <td>14</td>
                <td>This is a test again</td>
            </tr>
      </tbody>
</table>
<div id="entry1" class="largeWin">
        <a href="#" class="close">X</a>
      <p>some text here...</p>
</div>

And this is the JQuery part:

$('tr.popupOpen').click(function() {

    var popup= $(this).attr('data-href');

    $(popup).fadeIn(300);

    //Set the center alignment padding + border
    var popMargTop = ($(popup).height() + 24) / 2; 
    var popMargLeft = ($(popup).width() + 24) / 2; 

    $(popup).css({ 
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    // Add the mask to body
    $('body').append('<div id="mask"></div>');
    $('#mask').fadeIn(300);

    return false;
});

$('a.close, #mask').live('click', function() { 
  $('#mask , .largeWin').fadeOut(300 , function() {
    $('#mask').remove();  
}); 
return false;
});

Any ideas on how I can improve the centering and responsiveness of my popup?

Answer №1

Take a look at this code snippet.

See the Updated Code

Is this what you are aiming to achieve?

$(document).ready(function () {
    $('tr.popupOpen').click(function () {

        var popup = $(this).attr('data-href');

        $(popup).fadeIn(300);

        // Set the center alignment padding + border


        $(popup).css({
            'margin-top': '20px',
                'margin-left': '40px'
        });

        // Append the mask to body
        $('div.container').append('<div id="mask"></div>');
        $('#mask').fadeIn(300);

        return false;
    });

    $('a.close, #mask').live('click', function () {
        $('#mask , .largWin').fadeOut(300, function () {
            $('#mask').remove();
        });
        return false;
    });
});

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

Is it possible to exclude elements that are not something or something else using :not()?

Let the example do the talking .cyan { background-color: cyan; } .red { background-color: red; } .green { background-color: green; } .blue { background-color: blue; } .yellow { background-color: yellow; } li:not(.yellow), li:not(.green) { c ...

Utilizing jQuery to enable a div to follow the touchmove or mousemove events while also automatically

I am looking to create a basic function in my jQuery script. I want the pages to slide horizontally in response to the movement of a finger or cursor on the screen. While there are many plugins available for this purpose, I prefer a simple, customized solu ...

What are the steps to creating a duplicate of JotForm?

After exploring JotForm, I discovered it is an extremely interactive form builder utilizing the Prototype JS library. I am curious to know which JS framework or library would be a solid foundation for creating a similar form builder - JQuery, Prototype, ...

The div's innerHTML is consistently cleared after a php page loads, even though it loads successfully

When using jQuery, I am loading a div dynamically $('#Detailvolunteer').load("http://localhost/../somepage.php") After the content is loaded, the div appears to be empty even though it has been loaded successfully. The innerHTML value shows not ...

Discover the magic of using Polymer core-animated-pages transitions to create a stunning tile-cascade effect for your custom elements!

Trying to recreate the slide-up and tile-cascade transitions from this example using a snippet with the polymer designer tool. This time, custom elements are preferred over regular HTML tags. Custom elements have been created for each page and placed with ...

Issue with Bootstrap dropdown menu not closing correctly when clicked on mobile devices

On my Bootstrap website, I have a mobile menu that looks like this: <a class="nav-link" href="#" id="shop_submenu" role="button" data-bs-toggle="dropdown" aria-expanded="true" data-bs-auto-close="true"> Rent Costumes ...

The CSS3 box-shadow lacks a sense of dimension

I'm having trouble adding depth to a header div using CSS box-shadow. No matter what I try, all I get is a flat line instead of the desired shadow effect. I've experimented with different colors, but nothing seems to work. Any suggestions on how ...

Verify the presence of a username using the bvalidator, a jQuery validation plugin

Currently, I am using bvalidator, a jQuery validation plugin, to verify if a username exists in the database. The documentation provides an example that I tried implementing, but I am struggling to understand its functionality. For more details on this exa ...

Ensuring the secure extraction of HTML coding from messages

When extracting plaintext from messages containing valid and/or invalid HTML, as well as text resembling HTML (e.g. non-HTML text within <...> like: < why would someone do this?? >), it is crucial to retain all non-HTML content while attempting ...

The function only executes when the alert() function is called within its scope

Why does the output only appear when I include an alert inside the jQuery block? <script type="text/javascript"> (function map() { var test = document.createElement('script'); test.type = 'text/javascript'; test.async ...

Is there a way to customize the color of the collapsible menu?

I am currently delving into Bootstrap to enhance my skills. I decided to utilize the example navbar provided in the documentation: <nav class="navbar navbar-expand-lg navbar-light bg-primary"> <div class="container-fluid"&g ...

Eliminate the cushioning on a single item

I'm currently working on a website and running into a small issue. The main page has a background with a white body in the center, containing padding for the text to prevent it from being right at the border. #content { width: 900px; overflow: h ...

X/Y Client accessing via Chrome on Android device

I am looking to accurately capture the X and Y coordinates where the user taps on the screen, regardless of zoom or scroll levels. Currently, I am utilizing event.clientX and event.clientY to achieve this, which is working as intended. The code snippet loo ...

What is the correct way to utilize jquery's getJSON function?

<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#hidebtn").click(function() { ...

Why is it that I am receiving just one object as a string when making an ajax call, even though the controller is supposed to be returning

I'm having trouble understanding why my $.get call is returning one single object with a string containing my elements, instead of the expected list of objects returned by my controller. Controller: public JsonResult GetInitialTags(int id) { Mod ...

Tips on using b-table attributes thClass, tdClass, or class

<template> <b-table striped hover :fields="fields" :items="list" class="table" > </template> <style lang="scss" scoped> .table >>> .bTableThStyle { max-width: 12rem; text-overflow: ellipsis; } < ...

chart for visualizing two rows with matching IDs and dates

I have been attempting to accomplish the following two scenarios: 1) When both ID and dates are the same in the chart, but the descriptions differ, I want to display them on separate lines. 2) If there is not enough room to show the row label, I would li ...

Extract the date from the URL and update the chosen date on the jQuery datepicker

After some searching, I've come to ask a question here. I'm dealing with a jQuery datepicker within a form. When the user changes the date and clicks submit, the URL ends up looking like this: index.php?thedate=12%2F22%2F2012#, which really repr ...

Page Refresh Causes Angular Count to Reset to Zero

I'm facing an issue in my code where the counts for total and package get reset to 0 when the page is refreshed, even though labels have been scanned and added to the list. Below is a snippet of my code. Any suggestions on how to resolve this problem? ...

Label stops functioning properly when there is a line-height exceeding 1

The problem arises when the line-height of a paragraph is set to more than 1em and the label tag doesn't work properly between lines. <p style="line-height: 2em;"><input type="checkbox" id="xx" /> <label for="xx">Lorem ipsum dolor ...