I'm in the middle of building a multiple modal for my website that I'm working on. I've managed to start with something and currently I'm stuck in trying to make modals trigger separately from one another. It is basically how bootstrap does it by appending a data-attribute I believe. I could have just use bootstrap to solve my problem but I would like to avoid all the code bloat and just built it myself.
Here a sample code that I have been working on. I've managed to make the looping but it seems to open the wrong modal.
$('.modal-container').insertAfter('.layout-container');
$('.modal-container').each(function(i) {
$(this).attr('id', "modal" + (i + 1));
modal_container_id = $(this).attr('data-modal', "modal" + (i + 1));
});
$('.modal-item .modal-btn').each(function(i) {
var modal_id = $(this).attr('id', "modal" + (i + 1));
$(modal_id).on('click', function() {
$(modal_container_id).addClass('show-modal');
$('body').addClass('lock-scroll');
});
})
//Close Modal
$('.modal-container .close-modal').on('click', function() {
$('.modal-container').removeClass('show-modal');
$('body').removeClass('lock-scroll');
})
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline; }
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block; }
body {
line-height: 1; }
ol, ul {
list-style: none; }
blockquote, q {
quotes: none; }
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none; }
table {
border-collapse: collapse;
border-spacing: 0; }
.layout-container {
position: relative; }
.modal-container {
position: fixed;
top: 0;
left: 0;
z-index: 100;
visibility: hidden;
opacity: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
-webkit-transition: opacity 0.3s ease-in;
-moz-transition: opacity 0.3s ease-in;
transition: opacity 0.3s ease-in; }
.modal-container.show-modal {
visibility: visible;
opacity: 1;
-webkit-transition: opacity 0.3s ease-in;
-moz-transition: opacity 0.3s ease-in;
transition: opacity 0.3s ease-in; }
.modal-container .modal-body {
position: absolute;
max-width: 500px;
height: 500px;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
padding: 20px;
background-color: #fff; }
/*# sourceMappingURL=style.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="layout-container">
<div class="modal-item">
<button class="modal-btn">Modal One Trigger</button>
<div class="modal-container">
<button class="close-modal">Close Modal</button>
Modal One
</div>
</div>
<div class="modal-item">
<button class="modal-btn">Modal Two Trigger</button>
<div class="modal-container">
<button class="close-modal">Close Modal</button>
Modal Two
</div>
</div>
</div>
modal1/modal2as id to both the opening buttons and the modal div, and every id should be unique, change one of the prefix. Second, you don't select the good element in the click function, but you reuse a previous var$(modal_container_id). Use a selector based on the button id instead#i.e.$('#' + modal_id), However its better to use to DOM traversal method to target the desired elementIDon the button matches theIDon the.modal-containerclass it will trigger and show the modal