Home > Back-end >  How to create multiple modal in jquery?
How to create multiple modal in jquery?

Time:12-15

I am new in Jquery. I have a little jquery modal code for showing modal. But this modal only can use for one code. I mean, I cant create two modal from this code. For example;

Modal01 Modal02

Even if I create 2 modal, it's behave abnormal. I saw taht, data-id or any id or any atribute use for creating multiple modal. Where two part. Button contains a unique id or name and same name or id use for targeting modal.

Look at my code please:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<style>
.mi-modal {
    position: fixed;
    z-index: 10000; /* 1 */
    top: 0;
    left: 0;
    visibility: hidden;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}
.mi-modal.modal-visible {
    visibility: visible;
}
.mi-modal-overlay {
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: hsla(0, 0%, 0%, 0.4);
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s linear 0.3s, opacity 0.3s;
  box-sizing: border-box;  
}
.mi-modal.modal-visible .mi-modal-overlay {
  opacity: 1;
  visibility: visible;
  transition-delay: 0s;
}
.mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 6em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #fff;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
.mi-modal-transition {
  transition: all 0.4s;
  transform: translateY(-10%);
  opacity: 0;
}
.mi-modal.modal-visible .mi-modal-transition {
  transform: translateY(0);
  opacity: 1;
}
.mi-modal-header,
.mi-modal-content {
  padding: 1em;
}
.mi-modal-header {
  position: relative;
  background-color: #fff;
  box-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.06);
  border-bottom: 1px solid #e8e8e8;
}
.mi-modal-close {
  position: absolute;
  top: 0;
  right: 0;
  padding: 1em;
  color: #aaa;
  background: none;
  border: 0;
  font-size: 18px;
}
.mi-modal-close:hover {
  color: #777;
}
.mi-modal-heading {
  font-size: 1.125em;
  margin: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.mi-modal-content > *:first-child {
  margin-top: 0;
}
.mi-modal-content > *:last-child {
  margin-bottom: 0;
}
.mi-modal.modal-scroll .mi-modal-content{
max-height: 60vh;
overflow-y: scroll;
}
.mi-modal.modal-scroll .mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 2em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #CDf;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
</style>


<button >Show modal</button>
  <div >
    <div ></div>
    <div >
      <div >
        <button >&times;</button>
        <h2 >This is a modal</h2>
      </div>
      <div >
        <div >
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit eum delectus, libero, accusantium dolores inventore obcaecati placeat cum sapiente vel laboriosam similique totam id ducimus aperiam, ratione fuga blanditiis maiores.</p>
          <button >No</button>
          <button >Confirm</button>
        </div>
      </div>
    </div>
  </div>





<script>
$('.mi-modal-toggle').on('click', function(e) {
  e.preventDefault();
  $('.mi-modal').toggleClass('modal-visible');
});
</script>

How can I do it.......?

CodePudding user response:

There's many options; but the main principle is that you need separate HTML for each modal, which you don't have. You can have a single modal "frame" and then fill it with HTML, eg from a partial-view rendered server-side via an ajax call - but without that, and to start with, make sure you have two lots of mi-modal structure.

Then just give each one either a unique id or a unique class, with code for each one opening the related dialog. HTML element class= doesn't need to be limited to css/styles. eg

$('.mi-modal-toggle1').on('click', function(e) {
  $('.mi-modal1').toggleClass('modal-visible');
});
$('.mi-modal-toggle2').on('click', function(e) {
  $('.mi-modal2').toggleClass('modal-visible');
});

Start with

  • 2 sets of HTML for the modal
  • explicit buttons for each modal

Snippet:

// common close button
$('.mi-modal-toggle').click(function() {
  $(this).closest(".mi-modal").toggleClass('modal-visible');
});

// explicit button per modal
$('.mi-modal-toggle1').on('click', function(e) {
  $('.mi-modal1').toggleClass('modal-visible');
});
$('.mi-modal-toggle2').on('click', function(e) {
  $('.mi-modal2').toggleClass('modal-visible');
});
.mi-modal {
    position: fixed;
    z-index: 10000; /* 1 */
    top: 0;
    left: 0;
    visibility: hidden;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}
.mi-modal.modal-visible {
    visibility: visible;
}
.mi-modal-overlay {
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: hsla(0, 0%, 0%, 0.4);
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s linear 0.3s, opacity 0.3s;
  box-sizing: border-box;  
}
.mi-modal.modal-visible .mi-modal-overlay {
  opacity: 1;
  visibility: visible;
  transition-delay: 0s;
}
.mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 6em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #fff;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
.mi-modal-transition {
  transition: all 0.4s;
  transform: translateY(-10%);
  opacity: 0;
}
.mi-modal.modal-visible .mi-modal-transition {
  transform: translateY(0);
  opacity: 1;
}
.mi-modal-header,
.mi-modal-content {
  padding: 1em;
}
.mi-modal-header {
  position: relative;
  background-color: #fff;
  box-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.06);
  border-bottom: 1px solid #e8e8e8;
}
.mi-modal-close {
  position: absolute;
  top: 0;
  right: 0;
  padding: 1em;
  color: #aaa;
  background: none;
  border: 0;
  font-size: 18px;
}
.mi-modal-close:hover {
  color: #777;
}
.mi-modal-heading {
  font-size: 1.125em;
  margin: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.mi-modal-content > *:first-child {
  margin-top: 0;
}
.mi-modal-content > *:last-child {
  margin-bottom: 0;
}
.mi-modal.modal-scroll .mi-modal-content{
max-height: 60vh;
overflow-y: scroll;
}
.mi-modal.modal-scroll .mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 2em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #CDf;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button >Show modal</button>
<button >Show modal</button>

<div >
  <div ></div>
  <div >
    <div >
      <button >&times;</button>
      <h2 >This is a modal</h2>
    </div>
    <div >
      <div >
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit eum delectus, libero, accusantium dolores inventore obcaecati placeat cum sapiente vel laboriosam similique totam id ducimus aperiam, ratione fuga blanditiis maiores.</p>
        <button >No</button>
        <button >Confirm</button>
      </div>
    </div>
  </div>
</div>

<div >
  <div ></div>
  <div >
    <div >
      <button >&times;</button>
      <h2 >This is the second modal</h2>
    </div>
    <div >
      <div >
        <p>Nothing to see here</p>
        <button >No</button>
        <button >Confirm</button>
      </div>
    </div>
  </div>
</div>

You can then move onto data-driven code - ie one piece of code that will handle all/new modals without needing to change the code.

Give both the button and the modal html the same data- attribute and use js to locate them, eg:

<button  data-modal-id="modal1">Show modal</button>
<div  data-modal-id="modal1">
    $('.mi-modal-toggle').on('click', function(e) {
      var modalid = $(this).data("modal-id");
      $(`.mi-modal[data-modal-id='${modalid}']`).toggleClass('modal-visible');
    });

Now you can add as many button modal pairs as you like, without needing to change the js.

Note: if you add buttons/HTML after the page has opened (dynamically create them) then you'll need event delegation instead, eg:

$(document).on("click", ".mi-modal-toggle", function(e) {...

but otherwise it's the same. (for purists: use the closest static html node, rather than document)

Updated snippet:

// common close button
$('.mi-modal-toggle').click(function() {
  $(this).closest(".mi-modal").toggleClass('modal-visible');
});

// explicit button per modal
$('.mi-modal-toggle').on('click', function(e) {
  var modalid = $(this).data("modal-id");
  $(`.mi-modal[data-modal-id='${modalid}']`).toggleClass('modal-visible');
});
.mi-modal {
    position: fixed;
    z-index: 10000; /* 1 */
    top: 0;
    left: 0;
    visibility: hidden;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}
.mi-modal.modal-visible {
    visibility: visible;
}
.mi-modal-overlay {
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: hsla(0, 0%, 0%, 0.4);
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s linear 0.3s, opacity 0.3s;
  box-sizing: border-box;  
}
.mi-modal.modal-visible .mi-modal-overlay {
  opacity: 1;
  visibility: visible;
  transition-delay: 0s;
}
.mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 6em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #fff;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
.mi-modal-transition {
  transition: all 0.4s;
  transform: translateY(-10%);
  opacity: 0;
}
.mi-modal.modal-visible .mi-modal-transition {
  transform: translateY(0);
  opacity: 1;
}
.mi-modal-header,
.mi-modal-content {
  padding: 1em;
}
.mi-modal-header {
  position: relative;
  background-color: #fff;
  box-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.06);
  border-bottom: 1px solid #e8e8e8;
}
.mi-modal-close {
  position: absolute;
  top: 0;
  right: 0;
  padding: 1em;
  color: #aaa;
  background: none;
  border: 0;
  font-size: 18px;
}
.mi-modal-close:hover {
  color: #777;
}
.mi-modal-heading {
  font-size: 1.125em;
  margin: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.mi-modal-content > *:first-child {
  margin-top: 0;
}
.mi-modal-content > *:last-child {
  margin-bottom: 0;
}
.mi-modal.modal-scroll .mi-modal-content{
max-height: 60vh;
overflow-y: scroll;
}
.mi-modal.modal-scroll .mi-modal-wrapper {
  position: absolute;
  z-index: 9999;
  top: 2em;
  left: 50%;
  width: 32em;
  margin-left: -16em;
  background-color: #CDf;
  box-shadow: 0 0 1.5em hsla(0, 0%, 0%, 0.35);
  box-sizing: border-box;  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button  data-modal-id="modal1">Show modal</button>
<button  data-modal-id="secondmodal">Show modal</button>

<div  data-modal-id="modal1">
  <div ></div>
  <div >
    <div >
      <button >&times;</button>
      <h2 >This is a modal</h2>
    </div>
    <div >
      <div >
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit eum delectus, libero, accusantium dolores inventore obcaecati placeat cum sapiente vel laboriosam similique totam id ducimus aperiam, ratione fuga blanditiis maiores.</p>
        <button >No</button>
        <button >Confirm</button>
      </div>
    </div>
  </div>
</div>

<div  data-modal-id="secondmodal">
  <div ></div>
  <div >
    <div >
      <button >&times;</button>
      <h2 >This is the second modal</h2>
    </div>
    <div >
      <div >
        <p>Nothing to see here</p>
        <button >No</button>
        <button >Confirm</button>
      </div>
    </div>
  </div>
</div>

  • Related