What is the process for including next and previous buttons in a popup window?

I recently attempted to implement a next button in the popup box to run popups in a specific order. I also made sure that when the final popup opens, the button automatically disables itself. However, there seems to be an issue. When I delete certain modals, the code stops working. My goal is to have the popups run in the correct order even if some are deleted and also add a previous button. How can I achieve this? Any assistance in resolving this issue would be greatly appreciated.

Below is the code snippet I utilized:

$(document).ready(function() {
   var currentmodal = 1;
   $(".getAssignment").click(function() {
      var $divs = $(".modalDialog");
      var modal = $("*[data-modalorder="+(currentmodal++)+"]");
      if(!$("*[data-modalorder="+currentmodal+"]").length)
      {
          modal.find("input.getAssignment").prop("disabled",true);
      }
      if ($divs.length > 0 && modal) {
          window.location.href = "#" + $(modal).attr("id");
      }
   });
});

$(document).ready(function() {
       var currentmodal = 1;
       $(".getAssignment2").click(function() {
          var $divs = $(".modalDialog");
          var modal = $("*[data-modalorder="+(currentmodal--)+"]");
          if(!$("*[data-modalorder="+currentmodal+"]").length)
          {
              modal.find("input.getAssignment2").prop("disabled",true);
          }
          if ($divs.length > 0 && modal) {
              window.location.href = "#" + $(modal).attr("id");
          }
       });
    });

<input class="getAssignment" type="button" value="Open Modal">

<div id="openModal" class="modalDialog" data-modalorder=1>
    <div>
    <input class="getAssignment2" type="button" value="Previous">
    <input class="getAssignment" type="button" value="Next">
    <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 1</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

<div id="openModal2" class="modalDialog" data-modalorder=2>
    <div>   
   <input class="getAssignment2" type="button" value="Previous">
   <input class="getAssignment" type="button" value="Next">
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 2</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

https://jsfiddle.net/Sanjeewani/q1tm8ck2/38/

Answer №1

It seems like this might be close to what you're looking for, but I've made some changes to your JavaScript.

$(document).ready(function() {
var data=[];
  currentModal = 0;
  
  $('.modalDialog').each(function(){
    data.push({
      id: $(this).attr('id'),
      order: $(this).data('modalorder')
    });
  })
    
$('#openModalBtn').click(function(){
  currentModal = 0;
    window.location.href = "#" + data[currentModal].id;
    $('#'+data[currentModal].id).find('.getAssignment2').prop('disabled', true);
  })
  
  //prev
  $('.getAssignment2').click(function(){
  if (currentModal>0) {
    currentModal--;
      window.location.href = "#" + data[currentModal].id;
    } else {
      
    window.location.href = '#'
    }
  })
  
  //next
  $('.getAssignment').click(function(){
  if (currentModal<data.length - 1) {
    currentModal++;
      if (currentModal===data.length - 1) $('#'+data[currentModal].id).find('.getAssignment').prop('disabled', true);
      window.location.href = "#" + data[currentModal].id;
    } else {
    window.location.href = '#'
    }
  })
  
})
.modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}
.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}
.modalDialog > div {
    width: 400px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #fff;
    background: -moz-linear-gradient(#fff, #999);
    background: -webkit-linear-gradient(#fff, #999);
    background: -o-linear-gradient(#fff, #999);
}
.close {
    background: #606061;
    color: #FFFFFF;
    line-height: 25px;
    position: absolute;
    right: -12px;
    text-align: center;
    top: -10px;
    width: 24px;
    text-decoration: none;
    font-weight: bold;
    -webkit-border-radius: 12px;
    -moz-border-radius: 12px;
    border-radius: 12px;
    -moz-box-shadow: 1px 1px 3px #000;
    -webkit-box-shadow: 1px 1px 3px #000;
    box-shadow: 1px 1px 3px #000;
}
.close:hover {
    background: #00d9ff;
}
.getAssignment{
  cursor:pointer;
}
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<body>

<input type="button" id="openModalBtn" value="Open Modal">

<div id="openModal1" class="modalDialog" data-modalorder=1>
    <div>
    <input class="getAssignment2" type="button" value="Previous">
    <input class="getAssignment" type="button" value="Next">
    <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 1</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

<div id="openModal2" class="modalDialog" data-modalorder=2>
    <div>
   <input class="getAssignment2" type="button" value="Previous">
   <input class="getAssignment" type="button" value="Next">
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 2</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

<div id="openModal3" class="modalDialog" data-modalorder=3>
    <div>
      <input class="getAssignment2" type="button" value="Previous">
    <input class="getAssignment" type="button" value="Next">
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 3</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

</body>

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

Display JSON values from a successful AJAX call within an HTML dropdown menu

Having an html form with multiple drop down selections, I have utilized the same form for data editing, To edit data in a specific row of the table, I implemented an AJAX request. Once a row is selected and the user clicks on edit, the AJAX call retrieves ...

Guide on NodeJS: Harnessing the power of nested functions to ensure synchronous execution

Imagine two functions, A and B, both interacting with a MySQL database using connection.query(...) methods. Function A utilizes a while loop to navigate through the response it receives. Subsequently, function B is invoked with the response from function ...

The specified container is not a valid DOM element. (Utilizing Ant Design carousel component in React.js)

Here is a snippet of my code where I am having an issue with my carousel. It seems to be giving me an error message "Target container is not a DOM element" when I try to run the server. I have divided my code into two parts - the first part containing the ...

Challenges encountered when retrieving parameters from union types in TypeScript

Why can't I access attributes in union types like this? export interface ICondition { field: string operator: string value: string } export interface IConditionGroup { conditions: ICondition[] group_operator: string } function foo(item: I ...

Why do we use functions in JavaScript?

At what point does Javascript utilize Functions in its code? My understanding was: function anyfunc(){} I believed the following comparison would yield true: console.log(anyfunc === Function) However, it actually returns false. Why is that? Isn't an ...

Choose options with identical titles

If you click on the button to add more selects with the same name, I want to replace them so that you can access them in a PHP array. document.querySelector('#add').onclick = function () { var thespan = document.createElement('span&apos ...

Having difficulty positioning the dropdown above the other elements in the body

Below, you'll notice that the dropdown menu isn't positioned correctly over other body elements like the timer. I'm utilizing bootstrap for the dropdown and redcountdown js for the timer. https://i.sstatic.net/OO8Jm.png Here is the HTML: & ...

The use of the tooltip function in Rails 6 webpack may not be possible

I have attempted to modify the versions of bootstrap, jquery, and popper without success. I do not believe I am utilizing multiple versions of jquery. I am uncertain as to where the issue may lie. Any assistance in identifying what I may be overlooking wou ...

Sorry, I cannot provide that exact wording as it is verbatim from the original text. However, I can offer a unique version:

I am currently using request-promise in NodeJS to query 127.0.0.1:8443 with over 10,000 requests, and encountering the following error: { RequestError: Error: connect ECONNRESET 127.0.0.1:8443 and/or { RequestError: Error: read ECONNRESET When I reduce ...

I encountered a 404 Error message while attempting to access a specific express route

My Angular CLI generated app is running with an Express.js and MongoDB server. After running npm start, I can access http://localhost:3000, which routes to my homepage. The other links on the navbar work well to control routes, e.g., http://localhost:3000/ ...

Changes made in the view of a VueJS application are not being reflected in Laravel when using

Embarking on my first journey with VueJS within a Laravel PHP framework has been quite the adventure. A new project is on the horizon, and I dove in headfirst by making various changes, such as adding new elements and altering titles. However, much to my d ...

I'm having trouble getting my code to update after performing a search

Is there anyone out there who can lend a hand in pinpointing the troublesome code that's preventing my program from working? No matter how much I debug, I can't seem to get it right. It's been 3 hours of staring at this and I'm still st ...

jquery causing issues with browser functionality

I'm new to working with JQuery and currently in the process of deconstructing a code snippet I found online. The code functions perfectly fine on JSFiddle: JSFIDDLE. However, when attempting to run it in a browser, the section that should appear upon ...

Retrieve information from an external JSON API using jQuery

I need help with reading JSON data using jQuery. I have tried the code below but it doesn't seem to be working. This is the link to my JSON file: and here is my code snippet: $.getJSON("http://goo.gl/PCy2th", function(data){ $.each(data.PlayList ...

Structure for using Node modules

I've been having some trouble understanding how to set up Stripe for my app. I'm unsure about the implementation of the module in the paymentController file. Typically, when using a module, I would require it at the top of the file to use it effe ...

js expressive dynamic pattern matching

Hey there! I'm in the process of coding an online store and am currently focusing on implementing the add to cart functionality. My goal is to save product IDs and their quantities in a cookie, which will look something like this: id1:qt1,id2:qt2... I ...

The text alignment function of justify is not functioning properly

Trying to find a way to validate the content of this article, but I'm hitting a roadblock... <p style="text-align:justify;" class="custom-article"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tincidunt velit vitae risus t ...

Failure to trigger on change event

I am struggling with dynamically generating input fields of the file type. Unfortunately, when I attempt to access its triggered event, nothing seems to happen. Below is the code snippet for the dynamic input file: <div id="documentlist" class="col-lg ...

The Main page is being graced by a floating Twitter Bootstrap video

When trying to display a video next to a paragraph, I encountered an issue where the paragraph was taking up 100% of the screen and the video was floating over it, obstructing part of the text. Despite setting the row and cols, the layout wasn't behav ...

Utilize [markdown links](https://www.markdownguide.org/basic-syntax/#

I have a lengthy text saved in a string and I am looking to swap out certain words in the text with a highlighted version or a markdown link that directs to a glossary page explaining those specific words. The words needing replacement are contained within ...