Repeated information displayed in modal pop-ups

HTML

<a class="btn" data-popup-open="popup-1" href="#">More Details</a>

      <div class="popup" data-popup="popup-1">
        <div class="popup-inner">
        <h2>Unbelievable! Check this Out! (Popup #1)</h2>
        <p>Per Serving: 5g Energy: 20kcal Protein: 0.0g Total Fat: 0.0g Saturated Fat: 0.0g Carbohydrates: 0.0g Package Size: 1 x 24 x 350g</p>
        <p><a data-popup-close="popup-1" href="#">Close</a></p>
        <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
         </div>
      </div>

    <a class="btn" data-popup-open="popup-1" href="#">Quick Inquiry</a>

      <div class="popup" data-popup="popup-1">
        <div class="popup-inner">
        <h2>This One Won't Disappoint (Popup #1)</h2>
        <p>Another set of data that will not disappoint.</p>
        <p><a data-popup-close="popup-1" href="#">Close</a></p>
        <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
       </div>
     </div>
    

CSS:

/* Outer */
    .popup {
      width:100%;
      height:100%;
      display:none;
      position:fixed;
      top:0px;
      left:0px;
      background:rgba(0,0,0,0.75);
    }

    /* Inner */
    .popup-inner {
      max-width:700px;
      width:90%;
      padding:40px;
      position:absolute;
      top:50%;
      left:50%;
      -webkit-transform:translate(-50%, -50%);
      transform:translate(-50%, -50%);
      box-shadow:0px 2px 6px rgba(0,0,0,1);
      border-radius:3px;
      background:#fff;
    }

    /* Close Button */
    .popup-close {
      width:30px;
      height:30px;
      padding-top:4px;
      display:inline-block;
      position:absolute;
      top:0px;
      right:0px;
      transition:ease 0.25s all;
      -webkit-transform:translate(50%, -50%);
      transform:translate(50%, -50%);
      border-radius:1000px;
      background:rgba(0,0,0,0.8);
      font-family:Arial, Sans-Serif;
      font-size:20px;
      text-align:center;
      line-height:100%;
      color:#fff;
    }

    .popup-close:hover {
      -webkit-transform:translate(50%, -50%) rotate(180deg);
      transform:translate(50%, -50%) rotate(180deg);
      background:rgba(0,0,0,1);
      text-decoration:none;
    }
    

jQuery:

$(function() {
    //----- OPEN
    $('[data-popup-open]').on('click', function(e)  {
        var targeted_popup_class = jQuery(this).attr('data-popup-open');
        $('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);

        e.preventDefault();
    });

    //----- CLOSE
    $('[data-popup-close]').on('click', function(e)  {
        var targeted_popup_class = jQuery(this).attr('data-popup-close');
        $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

        e.preventDefault();
    });
    });
    

This code functions as expected but seems to have an issue with displaying the content in the second popup. Even after trying different jQuery popup implementations, the data inside the popup remains unchanged when navigating to the next one. How can this be resolved?

Answer №1

To customize the popups, you need to modify the data-popup attributes accordingly. For example, use popup-1 for the first popup and popup-2 for the second:

<a class="btn" data-popup-open="popup-1" href="#">More Details</a>

  <div class="popup" data-popup="popup-1">
    <div class="popup-inner">
    <h2>Exciting Stuff Here! (Popup #1)</h2>
    <p>Serving Size: 5g   Energy : 20kcal Protein : 0.0g Fat­Total : 0.0g  Saturated &nbsp; 0.0g  Carbohydrate : 0.0g Package Size : 1 x 24 x 350 g</p>
    <p><a data-popup-close="popup-1" href="#">Close</a></p>
    <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
     </div>
  </div>

<a class="btn" data-popup-open="popup-2" href="#">Quick inquiry</a>

  <div class="popup" data-popup="popup-2">
    <div class="popup-inner">
    <h2>This Feature Isn't Working(Popup #2)</h2>
    <p>Additional information that won't display</p>
    <p><a data-popup-close="popup-2" href="#">Close</a></p>
    <a class="popup-close" data-popup-close="popup-2" href="#">x</a>
   </div>
 </div>

Answer №2

If you want your webpage to function properly, make sure to alter the value of data-popup-open on the second <a> tag and match it with the corresponding data-popup attribute on the second <div class="popup">. Ensure that your JavaScript and CSS codes are correctly implemented.

<a class="btn" data-popup-open="popup-1" href="#">More Details</a>

<div class="popup" data-popup="popup-1">
  <div class="popup-inner">
    <h2>Wow! This is Awesome! (Popup #1)</h2>
    <p>Per Serve : 5g Energy : 20kcal Protein : 0.0g Fat­Total : 0.0g Saturated &nbsp; 0.0g Carbohydrate : 0.0g Package Size : 1 x 24 x 350 g</p>
    <p><a data-popup-close="popup-1" href="#">Close</a>
    </p>
    <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
  </div>
</div>

<!-- Changed data-popup-open to popup-2 -->
<a class="btn" data-popup-open="popup-2" href="#">Quick inquiry</a>
<!-- Change data-popup to popup-2 -->
<div class="popup" data-popup="popup-2">
  <div class="popup-inner">
    <h2>This is the one that wont work(Popup #1)</h2>
    <p>Another data that wont appear</p>
    <p><a data-popup-close="popup-1" href="#">Close</a>
    </p>
    <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
  </div>
</div>

Answer №3

Make sure to update the data-popup-open and data-popup attributes in the second modal section

<a class="btn" data-popup-open="popup-1" href="#">More Details</a>

  <div class="popup" data-popup="popup-1">
    <div class="popup-inner">
    <h2>Wow! This is Awesome! (Popup #1)</h2>
    <p>Per Serve : 5g   Energy : 20kcal Protein : 0.0g Fat­Total : 0.0g  Saturated &nbsp; 0.0g  Carbohydrate : 0.0g Package Size : 1 x 24 x 350 g</p>
    <p><a data-popup-close="popup-1" href="#">Close</a></p>
    <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
     </div>
  </div>

<a class="btn" data-popup-open="popup-2" href="#">Quick inquiry</a>

  <div class="popup" data-popup="popup-2">
    <div class="popup-inner">
    <h2>This one needs adjustments(Popup #2)</h2>
    <p>Other data that should be modified</p>
    <p><a data-popup-close="popup-2" href="#">Close</a></p>
    <a class="popup-close" data-popup-close="popup-2" href="#">x</a>
   </div>
 </div>

https://jsfiddle.net/873ww7pj/

Answer №4

Take a look at this code snippet, it's fully functional! To create more modals, simply change the ID or popup name.

$(function() {
//----- OPEN
$('[data-popup-open]').on('click', function(e)  {
    var targeted_popup_class = jQuery(this).attr('data-popup-open');
    $('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);

    e.preventDefault();
});

//----- CLOSE
$('[data-popup-close]').on('click', function(e)  {
    var targeted_popup_class = jQuery(this).attr('data-popup-close');
    $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

    e.preventDefault();
});
});
/* Outer */
.popup {
width:100%;
height:100%;
display:none;
position:fixed;
top:0px;
left:0px;
background:rgba(0,0,0,0.75);
}

/* Inner */
 .popup-inner {
max-width:700px;
width:90%;
padding:40px;
position:absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%, -50%);
transform:translate(-50%, -50%);
box-shadow:0px 2px 6px rgba(0,0,0,1);
border-radius:3px;
background:#fff;
}

/* Close Button */
.popup-close {
width:30px;
height:30px;
padding-top:4px;
display:inline-block;
position:absolute;
top:0px;
right:0px;
transition:ease 0.25s all;
-webkit-transform:translate(50%, -50%);
transform:translate(50%, -50%);
border-radius:1000px;
background:rgba(0,0,0,0.8);
font-family:Arial, Sans-Serif;
font-size:20px;
text-align:center;
line-height:100%;
color:#fff;
}

.popup-close:hover {
-webkit-transform:translate(50%, -50%) rotate(180deg);
transform:translate(50%, -50%) rotate(180deg);
background:rgba(0,0,0,1);
text-decoration:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="btn" data-popup-open="popup-1" href="#">More Details</a>

  <div class="popup" data-popup="popup-1">
    <div class="popup-inner">
    <h2>Wow! This is Awesome! (Popup #1)</h2>
    <p>Per Serve : 5g   Energy : 20kcal Protein : 0.0g Fat­Total : 0.0g  Saturated &nbsp; 0.0g  Carbohydrate : 0.0g Package Size : 1 x 24 x 350 g</p>
    <p><a data-popup-close="popup-1" href="#">Close</a></p>
    <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
     </div>
  </div>

<a class="btn" data-popup-open="popup-2" href="#">Quick inquiry</a>
  <div class="popup" data-popup="popup-2">
    <div class="popup-inner">
    <h2>This is the one that wont work(Popup #1)</h2>
    <p>Another data that wont appear</p>
    <p><a data-popup-close="popup-2" href="#">Close</a></p>
    <a class="popup-close" data-popup-close="popup-2" href="#">x</a>
   </div>
 </div>

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

JSF Ajax call: Invoking a JavaScript function at the completion

Working on a project with JSF 2.0, here is the form being used: <h:form id="fff" onsubmit="reloadMap();"> <h:selectOneMenu value="#{rentCarPoolBean.state}"> <f:selectItems value="#{rentCarPoolBean.stateList}" id="stateList" /> ...

What is the best way to initiate the registration page through the @auth0/auth0-react library?

I've hit a roadblock in trying to automatically launch the sign-up (registration) page using @auth0/auth0-react. Previously, I would send mode which worked with auth0-js. So far, I have attempted the following without success: const { loginWithRedir ...

How to extract a div from its parent using jQuery

I need to stack two div boxes on top of each other, and my goal is to hide the upper one when it's being hovered over. HTML: <div id="left_middle"> <div id="rfinder_UP"></div> <div id="rfinder_DWN"></div> </div> C ...

An error of undefined Angular Service/Factory has occurred

I created a factory service called siteCollection: spApp.factory('siteCollection', function(){ return { usersObject : [], getUsers : function (){ $().SPServices({ operation: "GetUserCollectionFromSite", completef ...

jQuery when - anticipate the fulfillment of multiple success/done callbacks

When using $.when to determine when an array of ajax promises are finished, I have noticed that although the ajax calls themselves are completed when $.when fires, their callbacks and done functions are not. How can I make sure to wait for the callbacks to ...

Transitioning between states in React with animation

In my React application, I have a menu that contains a sub-menu for each item. Whenever an item is clicked, the sub-menu slides in and the title of the menu changes accordingly. To enhance user experience, I aim to animate the title change with a smooth fa ...

Ways to verify the nodemon version that is currently installed on your local machine

On my Windows 10 machine, I recently installed nodemon locally in a project and now I'm curious to know which version is installed. Can someone please share the command to check the version of nodemon without needing to install it globally? My aim is ...

Creating a sleek grayscale effect when hovering with CSS is both simple and effective

My website features a grayscale logo that smoothly transitions to color upon hover. However, I am experiencing some issues with the smoothness of this transition when using CSS. Here is my current code: <img alt="TT ltd logo" src="./img/tt-logo.png" c ...

Is there a simpler method for making multiple posts using an AJAX JS loop?

My form is quite extensive and uses AJAX to save. The JavaScript functions are stored in an external file and structured like this: function milestone09() { $.post('./post.3.AIGs2GRA.php?data=' + (secData), $('#milestone09').serialize( ...

Eliminate all the zeros from the date string

Trying to work with a string that is in the format '01/02/2016' and my goal is to eliminate the leading zeros so I end up with '1/2/2016' using regex. So far, I have attempted '01/02/2016'.replace(/^0|[^\/]0./, '&ap ...

Utilizing tables for inquiries in email communication

Currently tackling a basic mailer with html. It seems like tables are recommended and inline styling is the safer route. However, I'm encountering an issue where there's a mysterious space when setting up my td and I can't seem to pinpoint ...

Error: JSON at position 1 is throwing off the syntax in EXPRESS due to an unexpected token "

I'm currently utilizing a REST web service within Express and I am looking to retrieve an object that includes the specified hours. var express = require('express'); var router = express.Router(); /* GET home page. ...

Remove checkbox in a selected option of a dropdown menu using JQuery

I apologize for the unusual title. Here's what I'm looking for: I have a dropdown menu with several options, and two of them are labeled as "special". If one of these special options is selected, three checkboxes should be disabled or hidden. B ...

Eliminate redundant sets of comma-separated numbers from the jQuery input text value

On my webpage, there is an input with a value that looks like this: 1,7,1,4,22,58,58,1,1,4,7 <input type="text" name="hello" id="thankyouforhelping" value="" aria-invalid="false"> I have attempted mu ...

Check for compatibility of overflow:scroll with mobile browsers

Is there an easy JavaScript method that works across different devices and libraries? I am looking to assign a class to the html element in order to enable scrollable containers on mobile devices when needed. I want to follow a similar approach to Modern ...

Change the order of the table data elements using the nth-child CSS selector

I need the second td in my table to appear on top of the first one when I resize the screen. @media screen and (min-width: 200px) and (max-width: 872px) { td :nth-child(1) {display:bottom} td :nth-child(2) {display:top} } <table border="0" cell ...

Unable to get Bootstrap IMG-RESPONSIVE to function properly in Firefox and IE within the navbar

I recently encountered an issue with the Bootstrap IMG-RESPONSIVE class not functioning properly in the navigation bar on Firefox and IE. Oddly enough, it was working perfectly just a few days ago until I made some unknown changes, causing it to only work ...

Creating an array of multiple divs based on numerical input

I am working on a project to show multiple divs based on the user's input number. For example, if the user selects 3, then 3 divs should be displayed. While I have successfully implemented this functionality, I need to dynamically assign IDs to each ...

How to Align Navigation Bar Items to the Right in Bootstrap 4

I need some help with bootstrap. I've looked at various discussions on this topic and tried numerous solutions, but I still can't get it right. I want the logo to be on the left and the navigation links aligned to the right. Here's what I&ap ...

How to dynamically add events to a JQuery FullCalendar using a loop

When utilizing the jQuery full calendar, I need to dynamically set events based on data obtained from a database. To achieve this, I am using data attributes to assign values and then display them on the calendar. Below is an example of my html twig code: ...