Ways to incorporate several modals into your interface

There are 2 modals on my website, but when I click on the second modal labeled "Mouthoff," it displays the contents of the Subscribe modal instead. I want each modal to display different content.

I have searched on various platforms including here and YouTube, but I am unable to find a solution.

// Get the modal
      var modal = document.getElementById("subscription");

      // Get the button that opens the modal
      var btn = document.getElementById("subscribe");

      // Get the <span> element that closes the modal
      var span = document.getElementsByClassName("close")[0];

      // When the user clicks the button, open the modal 
      btn.onclick = function() {
        modal.style.display = "block";
      }

      // When the user clicks on <span> (x), close the modal
     ...

Answer №1

It's important to use unique variable names for different QuerySelectors in order to access and manipulate them without reassigning.

If you do end up reassigning, keep in mind that only the most recent assignment will be considered while accessing the elements.

// Grab the modal
var modal = document.getElementById("subscription");

// Access the button that triggers the modal
var subscribeBtn = document.getElementById("subscribe");

// Get the <span> tag responsible for closing the modal
var closeSpan = document.getElementsByClassName("close")[0];

// Retrieve another modal element
var secondaryModal = document.getElementById("shout");

// Obtain the button associated with the secondary modal
var shoutButton = document.getElementById("mouthoff");

// Fetch the <span> used for closing the secondary modal
var closeSecondary = document.getElementsByClassName("close")[0];

// Show the initial modal when its respective button is clicked  
subscribeBtn.onclick = function() {
  modal.style.display = "block";
}

// Close the initial modal upon clicking the close <span>
closeSpan.onclick = function() {
  modal.style.display = "none";
}


// Display the secondary modal upon button click
shoutButton.onclick = function() {
  secondaryModal.style.display = "block";
}

// Hide the secondary modal when the close <span> is clicked
closeSecondary.onclick = function() {
  secondaryModal.style.display = "none";
}

// If user clicks outside the modals, close them
window.onclick = function(event) {
  if (event.target == modal || event.target == secondaryModal) {
    secondaryModal.style.display = "none";
    modal.style.display = "none";
  }
}
button.subscribe {
  background: transparent !important;
  border: none;
  display: inline-block;
  zoom: 1;
  *display: inline;
  vertical-align: top;
  float: none !important;
  margin-top: 7px;
  text-transform: uppercase;
}
...
...
<!-- Trigger/Open The Modal -->
<button class="subscribe" id="subscribe">Subscribe</button>
...
...

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

Issue: Request from a different origin blocked

I encountered an issue while working on a web project using the PlanGrid API. I am receiving a cross-domain request block error. var apiKey="API KEY"; var password="PASSWORD"; $.ajax({ url: "https://io.plangrid.com/projects", xhrFields: { ...

Transferring JSON Data into JavaScript Variables

I've been struggling to identify what's causing the issue with the code below. It seems that the values from values.json are not being loaded into the variable statesau correctly. When I use document.write(JSON.stringify(statesau)), I just get an ...

Create a division that will remain visible on the screen and allow scrolling when a certain class is

Having some trouble with a fixed class div that is not continuing to scroll after the class is removed on scroll. I've attempted to fix this issue but the div keeps getting hidden instead of continuing to scroll. If anyone has any advice or can poin ...

Placing error notifications beneath my table rows for a more polished appearance

In the process of creating a review page for my app that showcases uploaded files, I've encountered an issue. Whenever a user uploads files with errors, I want to display a warning message below each specific file. Currently, I'm able to show the ...

Autonomous boundary for list elements

I have a specific issue that can be observed in this fiddle: http://jsfiddle.net/h0qu4ffv/ where I set the margin-top of one list item to 50px, but all list items end up following this style. Is there a way to give each list item an independent margin, or ...

Implementing dynamic data binding in JavaScript templates

I've been experimenting with jQuery and templates, and I managed to create a basic template binding system: <script type="text/template" id="Template"> <div>{0}</div> </script> Furthermore... var buffer = ''; v ...

Update the labelClass of markers when hovering over them with jQuery on Google Maps

After successfully incorporating the markerwithlabel.js file, I am now able to display markers with labels on my Google map using the following code: markerArray[i] = new MarkerWithLabel({ position: myLatLng, map: map, icon: image, lab ...

Should I consider using Derby.js or Meteor for a production app with authentication capabilities?

Recently, I delved into researching Derby.js and Meteor for a project I'm currently working on. Both frameworks offer real-time functionalities that could be quite useful. However, I have some reservations and am contemplating whether it's the ri ...

As I hover over this bootstrap button with the class "btn-info", its background color fades away

Can anyone explain why this button loses its background color when I hover over it? Here is the code for the button: <nav class="navbar navbar-default navbar-static-top" role="navigation"> <div class="container"> <button type="butt ...

Enhance the appearance of your Vuejs application with conditional style formatting

I want to enhance a basic table by applying conditional CSS styles to the cells based on their values. For example: If area1 == 'one', then assign the td-one CSS style. <tr v-for="version in versions" :key="version.id"> < ...

Tips for dynamically changing the color of a React Native style based on data conditions

Just starting with react native. I'm working on a basic quiz feature for my app. I've created an api that provides questions, answers, and a boolean value indicating the correct answer, Something like this : { "question:"Demo quest ...

Unleashing the power of RollupJs: A guide to dynamically bundling modules and objects

Is there a way to dynamically bundle a module/object into my RollupJs output file? I have experimented with various options without success in achieving the desired result. Below is a brief sample project that demonstrates what I am trying to achieve. The ...

Steps for positioning one div below another:1. Set the position property

Indeed, I am aware that this question has been asked numerous times in the past. Despite trying other solutions, none seem to resolve my issue. My objective is to have the "register" div slide down beneath the "container" div, rather than appearing next t ...

I am on a quest to locate the xpath for a day that is divided by the HTML line break element,

In my HTML structure, I have the following code snippet: <td class="FormLabel" valign="top"> <span class="DataLabel">Consists of:</span> </td> <td class="FormData"> <span class="BodyText"> Sunday<br> M ...

When a jquery confirm dialog is displayed, the scroll bar mysteriously fades away

Whenever a jQuery confirm dialog is displayed, the scroll bar on the page disappears, causing the page to suddenly shift to the right. Here is the JavaScript code for the dialogue: <script src="vendor/jquery-confirm.min.js"></script> <link ...

Retrieve a single record in Angular/Typescript and extract its ID value

There is data stored in a variable that is displayed in the Chrome console like this: 0: @attributes: actPer: "1", id: "19" 1: @attributes: actPer: "1" id: "17" etc To filter this data, the following code was used: myvar = this.obj.listR ...

How can we transfer parameters in JavaScript?

My vision may be a bit vague, but I'll try to explain it as best as I can. I want to implement multiple buttons that can toggle the visibility of a div (I have this functionality working already). Each button should carry two values (a number and a l ...

I'm having trouble resolving this issue - Internal server error: Cannot redefine property: File. Can anyone help me out with a solution?

Every time I try to use Vite with React, I encounter a persistent problem. Despite my efforts to find a solution by researching extensively and attempting to adjust the Node and NPM versions, the issue persists. ➜ Local: http://localhost:3000/ ➜ ...

Use Jquery to add unique styles to specific words within a paragraph

I am looking to specifically style a particular word within a dynamic div for example, <div id="info">{$info}</div> prints <p>here you can get info about somnething. if you want more info about something then click here....</p> ...

AngularJS implementation for a confirmation dialog with data

I need help creating a confirmation dialog box for user action verification. Here's the situation: I have a table with multiple events, and users can choose to delete an event. This is how the table is structured: <tbody> <tr ng-repeat= ...