Create a hidden form, then on click of a button toggle the form using .show() and .hide()
$('#show').on('click', function () {
$('.center').show();
$(this).hide();
})
$('#close').on('click', function () {
$('.center').hide();
$('#show').show();
})
.center {
margin: auto;
width: 60%;
padding: 20px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.hideform {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center hideform">
<button id="close" style="float: right;">X</button>
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
<button id="show">Show form</button>
Answer from Junius L on Stack OverflowVideos
Is Popupsmart Compatible with CMS to Display Popup Campaign?
How Can I Ensure a Seamless UX with Popups?
Can I Track the Performance of My Popups?
Create a hidden form, then on click of a button toggle the form using .show() and .hide()
$('#show').on('click', function () {
$('.center').show();
$(this).hide();
})
$('#close').on('click', function () {
$('.center').hide();
$('#show').show();
})
.center {
margin: auto;
width: 60%;
padding: 20px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.hideform {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center hideform">
<button id="close" style="float: right;">X</button>
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
<button id="show">Show form</button>
extremely simple using jquery:
$('.open-form').click(function(){
if (!$(this).hasClass('open')){
$('.form').css('display','block')
$(this).addClass('open');
$(this).text('CLOSE FORM');
}
else{
$('.form').css('display','none')
$(this).removeClass('open');
$(this).text('OPEN FORM');
}
});
input{
display:block;
margin-bottom:10px;
}
.parent{
position: relative;
height: 100vh;
}
form{
position:absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
-webkit-transform: translate(-50%,-50%);
display:none;
padding: 20px;
background-color: lightgray;
}
.open-form{
display: absolute;
top: 10px;
left: 10px;
cursor: pointer;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
<form class="form">
<label for="name">name</label>
<input id="name" type="text">
<label for="last-name">last name</label>
<input id="last-name" type="text">
<label for="city">city</label>
<input id="city" type="text">
</form>
<div class="open-form">OPEN FORM</div>
</div>
Use this code
HTML
<div id="divdeps" style="display:none" title=""></div>
Jquery on DOM ready
$("#divdeps").dialog({
autoOpen: false,
show: 'slide',
resizable: false,
position: 'center',
stack: true,
height: 'auto',
width: 'auto',
modal: true
});
This code will initialize a Dialog and put it in state ready to be open and successively closed. If you want to open the dialog when the page loads then add this line of code just after the code you've already added in document ready:
$("#divdeps").dialog('open');
If instead you want to open the Dialog following a click event add the same code on the click event of the element that should fire the opening.
Add your form inside the myDialog DIV. If you need more help regarding the form submission just give us more details...
How to generate a simple popup using jQuery
- Fixed syntax error by adding missing closing paren
). - Removed the inline
onclickhandler from theaddBookbutton. - Removed the unnecessary event handler param
openForm. - Moved the
id="popUpForm"to the form's parent div.
//define button and form//
const popUpForm = document.getElementById("popUpForm");
var button = document.getElementById("addBook");
//Form Pop-Up//
//button.onclick = () => {window.open('hello!')};//
//button function//
button.addEventListener("click", function() {
document.getElementById("popUpForm").style.display = "block";
});
h1 {
font-family: ohno-blazeface, sans-serif;
font-weight: 100;
font-style: normal;
font-size: 8vh;
color: #001D4A;
}
.head-box {
background-color: #9DD1F1;
display: flex;
justify-content: center;
}
h2 {
font-family: poppins, sans-serif;
font-weight: 300;
font-style: normal;
font-size: 3vh;
color: #c2e8ff;
}
button {
height: 10vh;
width: 20vh;
font-size: 3vh;
background-color: #27476E;
border-radius: 22px;
border-color: #daf1ff;
border-width: 2px;
border-style: solid;
}
button:hover {
background-color: #192c44;
}
body {
background-color: #9DD1F1;
}
.body-box {
display: flex;
justify-content: center;
}
/* The pop up form - hidden by default */
.form-popup {
display: none;
position: fixed;
bottom: 0;
right: 15px;
border: 3px solid #f1f1f1;
z-index: 9;
}
.form-container {
display: block;
max-width: 300px;
padding: 10px;
}
<div class="head-box">
<h1>My Library</h1>
</div>
<div class="body-box">
<button id="addBook"><h2>Add Book</h2></button>
</div>
<!-----Form information----->
<div class="form-popup" id="popUpForm">
<form action="example.com/path" class="form-container">
<input type="text" id="title" placeholder="Title">
<input type="author" id="author" placeholder="Author">
<input type="pages" id="pages" placeholder="Pages">
<input type="checkbox" id="readOption" name="readOption">
<label for="readOption">Have you read it?</label>
<button type="submit">Submit</button>
</form>
</div>
const popUpForm = document.querySelector(".form-popup");
const button = document.querySelector("#addBook");
button.addEventListener("click", () => {
popUpForm.style.display = "block";
});
h1 {
font-family: ohno-blazeface, sans-serif;
font-weight: 100;
font-style: normal;
font-size: 8vh;
color: #001D4A;
}
.head-box {
background-color: #9DD1F1;
display: flex;
justify-content: center;
}
h2 {
font-family: poppins, sans-serif;
font-weight: 300;
font-style: normal;
font-size: 3vh;
color: #c2e8ff;
}
button {
height: 10vh;
width: 20vh;
font-size: 3vh;
background-color: #27476E;
border-radius: 22px;
border-color: #daf1ff;
border-width: 2px;
border-style: solid;
}
button:hover {
background-color: #192c44;
}
body {
background-color: #9DD1F1;
}
.body-box {
display: flex;
justify-content: center;
}
/* The pop up form - hidden by default */
.form-popup {
display: none;
position: fixed;
bottom: 0;
right: 15px;
border: 3px solid #f1f1f1;
z-index: 9;
}
.form-container {
display: block;
max-width: 300px;
padding: 10px;
}
<div class="head-box">
<h1>My Library</h1>
</div>
<div class="body-box">
<button id="addBook"><h2>Add Book</h2></button>
</div>
<!-----Form information----->
<div class="form-popup">
<form action="example.com/path" class="form-container" id="popUpForm">
<input type="text" id="title" placeholder="Title">
<input type="author" id="author" placeholder="Author">
<input type="pages" id="pages" placeholder="Pages">
<input type="checkbox" id="readOption" name="readOption">
<label for="readOption">Have you read it?</label>
<button type="submit">Submit</button>
</form>
</div>
Please try jQuery UI dialog
Here is the forms demo
For mobile use, have a look at jQuery Mobile - Creating dialogs
Live Demo
Sounds like you might want a light box,and since you didnt tag your question with jQuery included is a pure JS example of how to make one.
JS
var opener = document.getElementById("opener");
opener.onclick = function(){
var lightbox = document.getElementById("lightbox"),
dimmer = document.createElement("div");
dimmer.style.width = window.innerWidth + 'px';
dimmer.style.height = window.innerHeight + 'px';
dimmer.className = 'dimmer';
dimmer.onclick = function(){
document.body.removeChild(this);
lightbox.style.visibility = 'hidden';
}
document.body.appendChild(dimmer);
lightbox.style.visibility = 'visible';
lightbox.style.top = window.innerHeight/2 - 50 + 'px';
lightbox.style.left = window.innerWidth/2 - 100 + 'px';
return false;
}
Markup
<div id="lightbox">Testing out the lightbox</div>
<a href="#" id="opener">Click me</a>
CSS
#lightbox{
visibility:hidden;
position:absolute;
background:red;
border:2px solid #3c3c3c;
color:white;
z-index:100;
width: 200px;
height:100px;
padding:20px;
}
.dimmer{
background: #000;
position: absolute;
opacity: .5;
top: 0;
z-index:99;
}