I am starting a project and I envision needing lots and lots of dialogs/modals.
I came up with some very rudimentary code that just basically shows the dialog when the button is pressed and then hides it when the cancel button is pressed.
The only problem I have now is that I can continue with this method although it would mean duplicating the same bit of code over and over with the ID of every single dialog.
Is there a way of just having one bit of script that maybe takes the a href value? or some way I can put an id on the a tag and that will bring up the relevant popup with the same ID?
Or maybe a cleaner and more efficient way of doing this?
Here is the code I am using:
HTML:
<a id="create">New</a>
<a id="edit">Edit</a>
<div class="overlay"></div>
<div class="dialog" id="create-new-dialog">
<header>
<h1>Create New</h1>
</header>
<div class="dialog-content">
<p>I am some content info</p>
</div>
<footer>
<div class="inner-container">
<p></p>
<a id="exit" class="btn-dark-outline dialog-btn cancel">Cancel</a>
</div>
</footer>
</div>
<div class="dialog" id="edit-dialog">
<header>
<h1>Edit</h1>
</header>
<div class="dialog-content">
<p>I am some content info</p>
</div>
<footer>
<div class="inner-container">
<p></p>
<a id="exit" class="btn-dark-outline dialog-btn cancel">Cancel</a>
</div>
</footer>
</div>
CSS:
a {
background:lightpink;
padding:20px;
cursor:pointer;
}
.overlay {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
background:black;
display:none;
opacity:0.5;
}
.dialog {
background:green;
position:fixed;
width:50%;
margin-left:25%;
border:1px solid #000000;
z-index:999999;
display:none;
top:25%;
}
.dialog header {
min-height:50px;
background-color:#e9e9e9;
text-align:center;
border-bottom:1px solid #c4c4c4;
}
.dialog header h1 {
margin:0;
font-weight:300;
font-size:22px;
padding-top:10px;
}
.dialog .dialog-content {
background-color:#f7f7f7;
min-height:200px;
padding:20px;
margin:0;
}
.dialog footer {
position:absolute;
bottom:0;
background-color:#ececec;
width:100%;
border-top:1px solid #acacac;
}
.dialog footer .inner-container {
padding:10px;
text-align:right;
}
.dialog footer .inner-container p {
float:left;
width:300px;
text-align:left;
margin:0;
}
footer .dialog-btn {
background:#ffffff !important;
color:#000000 !important;
padding:13px;
display:inline-block;
}
SCRIPT:
$(".cancel").click(function(){
$('.dialog').stop().fadeOut(200);
$('.overlay').hide();
});
$("#create").click(function(){
$('#create-new-dialog').stop().fadeIn(300);
$('.overlay').fadeIn(300);
});
$("#edit").click(function(){
$('#edit-dialog').stop().fadeIn(300);
$('.overlay').fadeIn(300);
});
Here is the example I have been working on: https://jsfiddle.net/susannalarsen/28t6t51x/
You could use data attributes in the links:
the links:
And a fiddle
You can also like you said use the href attribute:
html: