I am designing a web page. When we click the content of div named mail, how can I show a popup window containing a label email and text box?
-
1I found this [answer](http://stackoverflow.com/a/15466856/240803) very useful for quick alerts without touching the existing HTML or CSS. It creates and shows a div just using jQuery from js. – mabi Jun 06 '16 at 08:24
12 Answers
First the CSS - tweak this however you like:
a.selected {
background-color:#1F75CC;
color:white;
z-index:100;
}
.messagepop {
background-color:#FFFFFF;
border:1px solid #999999;
cursor:default;
display:none;
margin-top: 15px;
position:absolute;
text-align:left;
width:394px;
z-index:50;
padding: 25px 25px 20px;
}
label {
display: block;
margin-bottom: 3px;
padding-left: 15px;
text-indent: -15px;
}
.messagepop p, .messagepop.div {
border-bottom: 1px solid #EFEFEF;
margin: 8px 0;
padding-bottom: 8px;
}
And the JavaScript:
function deselect(e) {
$('.pop').slideFadeToggle(function() {
e.removeClass('selected');
});
}
$(function() {
$('#contact').on('click', function() {
if($(this).hasClass('selected')) {
deselect($(this));
} else {
$(this).addClass('selected');
$('.pop').slideFadeToggle();
}
return false;
});
$('.close').on('click', function() {
deselect($('#contact'));
return false;
});
});
$.fn.slideFadeToggle = function(easing, callback) {
return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
};
And finally the html:
<div class="messagepop pop">
<form method="post" id="new_message" action="/messages">
<p><label for="email">Your email or name</label><input type="text" size="30" name="email" id="email" /></p>
<p><label for="body">Message</label><textarea rows="6" name="body" id="body" cols="35"></textarea></p>
<p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p>
</form>
</div>
<a href="/contact" id="contact">Contact Us</a>
Here is a jsfiddle demo and implementation.
Depending on the situation you may want to load the popup content via an ajax call. It's best to avoid this if possible as it may give the user a more significant delay before seeing the content. Here couple changes that you'll want to make if you take this approach.
HTML becomes:
<div>
<div class="messagepop pop"></div>
<a href="/contact" id="contact">Contact Us</a>
</div>
And the general idea of the JavaScript becomes:
$("#contact").on('click', function() {
if($(this).hasClass("selected")) {
deselect();
} else {
$(this).addClass("selected");
$.get(this.href, function(data) {
$(".pop").html(data).slideFadeToggle(function() {
$("input[type=text]:first").focus();
});
}
}
return false;
});

- 31,495
- 6
- 74
- 83
-
Its Working, Thanks. But im going to include another button call register. when it is clicked, the registration form should popup. For that i included the same function and changed the name of ids and classes. The problem is when i click the close button of the registration form, it toggles the contact form. Need help @jason Davis – Rajasekar Aug 26 '09 at 07:51
-
1to remove the html you add on close change the close method to $(".close").live('click', function() { $(".pop").slideFadeToggle(); $("#contact").removeClass("selected"); $(".pop").remove(); //add this... return false; }); this will correct the problem yo get when you click on the link more then once before closing the popup. nice answer by the way, slick and simple... – Jonx May 25 '10 at 23:10
-
10@yahelc Try clicking "Sign Up" more than once consecutively, then click "Cancel". Uh-oooohhhh. – AVProgrammer Oct 05 '11 at 00:18
-
I don't quite agree on the "great code" part. A lot of content is hardcoded into javascript. Yes we all have javascript in our browsers there days, but simply it is not the right way to do web. HTML is for content, js for "makeup". Karim79 answer is better from my perspective. Include your markup in the HTML where it belongs, hide it, and just show it and bring it up as popup. Much cleaner solution than printing a lot of HTML directly from javascript. You don't have to use the jQueryUI if you don't need it, there are a million ways to put some content in a popup – ZolaKt Dec 19 '11 at 09:32
-
@ZolaKt As mentioned in my answer, "The appending of the div is an ajax call in my app - you might want to change the append code.". The focus isn't on how you're loading content. – Andy Gaskell Dec 19 '11 at 16:34
-
@AndyGaskell Ok, I missed that, sorry. Nevertheless, printing markup from javascript is a poor design IMHO. If you agree you should point it out in your answer, when some other newbie stumbles on the same issue. – ZolaKt Dec 19 '11 at 17:51
-
@AndyGaskell The pop is sleek, simple, lighweight and awesome. I need some help, can you give me an example of appending the div as an ajax call. I am currently looking for something similar to that just what you have mentioned in the above comment. – user525146 May 24 '12 at 20:05
-
@user525146 Yes, I've been meaning to update this answer since it's now a few years old. I'll try to get to it tonight. – Andy Gaskell May 24 '12 at 21:24
-
@AndyGaskell Can you please give an example when you have some time. Thanks! – user525146 May 29 '12 at 15:58
-
@AndyGaskell This is great Andy. However I need to be able to use multiple popups in a single page. Any clues as to how I might achieve this? – Rory Becker May 30 '12 at 09:51
-
12According to [jQuery website](http://api.jquery.com/live/), **As of jQuery 1.7, the .live() method is deprecated. Use [.on()](http://api.jquery.com/on/) to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().**. So I replaced *.live* with *.on*. [Click here to see a more generic version of Andy's code](http://jsfiddle.net/h7pxU/835/). Also, I used CDN link for more recent version of jQuery on the actual page `` – vulcan raven Sep 04 '12 at 06:25
-
This is all very nice but you say 'it doesnt need a plugin'. But that's exactly what this IS - it's code that belongs in a plugin. There's no minimum size requirements for a plugin. – Simon_Weaver Jan 30 '13 at 22:21
-
Forked fiddle to allow multiple popups without repeating the same core JQuery code. http://jsfiddle.net/N4QCZ/ h/t to @KevSheedy here: http://stackoverflow.com/questions/14836965/reducing-duplicated-code-with-jquery-function/14837043#14837337 – a coder Feb 12 '13 at 16:56
-
@vulcanraven It's no wonder the code wouldn't work with a newer version of jquery. – JJJ Jun 18 '14 at 20:24
-
Hey @JosanIracheta, I just updated the fiddle along with the sample code. It uses jQuery 2.1.0. vulcanraven's code has a subtle bug with deselect. – Andy Gaskell Jun 18 '14 at 20:54
-
Well all that had to be changed was `live` to 'on'. Thanks for the code. I'm using it on one of my sites;) – JJJ Jun 18 '14 at 23:36
-
Pop-up confirm window using jQuery only (no jQueryUI), I made it a plugin http://jsfiddle.net/B92M5/ – Adriano Jul 16 '14 at 08:52
-
Is there any chance that we can make the pop up adjust according current position on screen? I mean if there is no space in the window it'll pop above the button else below the button. Thank You – Jnanaranjan Feb 05 '16 at 14:21
-
@AndyGaskell On Ajax version a parenthesis is missing for $.get - Uncaught SyntaxError: missing ) after argument list – bhu1st Nov 24 '17 at 09:24
Check out jQuery UI Dialog. You would use it like this:
The jQuery:
$(document).ready(function() {
$("#dialog").dialog();
});
The markup:
<div id="dialog" title="Dialog Title">I'm in a dialog</div>
Done!
Bear in mind that's about the simplest use-case there is, I would suggest reading the documentation to get a better idea of just what can be done with it.

- 339,989
- 67
- 413
- 406
-
-
9@Rajasekar - You will need to download the jQuery UI bundle and at the very least include ui.core.js and ui.dialog.js to get a Dialogue. If you want the dialogue to be draggable and/or resizable then you will need to include ui.draggable.js and ui.resizable.js. – karim79 Aug 25 '09 at 15:00
-
6
I use a jQuery plugin called ColorBox, it is
- Very easy to use
- lightweight
- customizable
- the nicest popup dialog I have seen for jQuery yet

- 30,738
- 21
- 105
- 131

- 48,204
- 100
- 318
- 537
I think this is a great tutorial on writing a simple jquery popup. Plus it looks very beautiful

- 11,355
- 3
- 53
- 66
Let's try .... How to create a simple popup by using HTML, CSS, and jquery ...
$(function() {
// Open Popup
$('[popup-open]').on('click', function() {
var popup_name = $(this).attr('popup-open');
$('[popup-name="' + popup_name + '"]').fadeIn(300);
});
// Close Popup
$('[popup-close]').on('click', function() {
var popup_name = $(this).attr('popup-close');
$('[popup-name="' + popup_name + '"]').fadeOut(300);
});
// Close Popup When Click Outside
$('.popup').on('click', function() {
var popup_name = $(this).find('[popup-close]').attr('popup-close');
$('[popup-name="' + popup_name + '"]').fadeOut(300);
}).children().click(function() {
return false;
});
});
body {
font-family:Arial, Helvetica, sans-serif;
}
p {
font-size: 16px;
line-height: 26px;
letter-spacing: 0.5px;
color: #484848;
}
/* Popup Open button */
.open-button{
color:#FFF;
background:#0066CC;
padding:10px;
text-decoration:none;
border:1px solid #0157ad;
border-radius:3px;
}
.open-button:hover{
background:#01478e;
}
.popup {
position:fixed;
top:0px;
left:0px;
background:rgba(0,0,0,0.75);
width:100%;
height:100%;
display:none;
}
/* Popup inner div */
.popup-content {
width: 700px;
margin: 0 auto;
box-sizing: border-box;
padding: 40px;
margin-top: 100px;
box-shadow: 0px 2px 6px rgba(0,0,0,1);
border-radius: 3px;
background: #fff;
position: relative;
}
/* Popup close button */
.close-button {
width: 25px;
height: 25px;
position: absolute;
top: -10px;
right: -10px;
border-radius: 20px;
background: rgba(0,0,0,0.8);
font-size: 20px;
text-align: center;
color: #fff;
text-decoration:none;
}
.close-button:hover {
background: rgba(0,0,0,1);
}
@media screen and (max-width: 720px) {
.popup-content {
width:90%;
}
}
<!DOCTYPE html>
<html>
<head>
<title> Popup </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<a class="open-button" popup-open="popup-1" href="javascript:void(0)"> Popup
Preview</a>
<div class="popup" popup-name="popup-1">
<div class="popup-content">
<h2>Title of Popup </h2>
<p>Popup 1 content will be here. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam consequat diam ut tortor
dignissim, vel accumsan libero venenatis. Nunc pretium volutpat
convallis. Integer at metus eget neque hendrerit vestibulum.
Aenean vel mattis purus. Fusce condimentum auctor tellus eget
ullamcorper. Vestibulum sagittis pharetra tellus mollis vestibulum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<a class="close-button" popup-close="popup-1" href="javascript:void(0)">x</a>
</div>
</div>
</body>
</html>

- 147
- 4
-
i wanna ask.. this answer is very helpful for me.. but i curious.. how could popup change display:block when we clicked the trigger? cause for example above there's no one command to change it.. thank you.. – Heru Wijayanto Jul 12 '21 at 08:00
There is a good, simple example of exactly this, here: http://www.queness.com/post/77/simple-jquery-modal-window-tutorial

- 5,551
- 8
- 37
- 49
Extremely Lightweight Modal popup plugin. POPELT - http://welbour.com/labs/popelt/
It is lightweight, supports nested popups, object oriented, supports dynamic buttons, responsive, and lot more. Next update will include Popup Ajax form submissions etc.
Feel free to use and tweet feedback.

- 950
- 2
- 8
- 20
Simple popup window by using html5 and javascript.
html:-
<dialog id="window">
<h3>Sample Dialog!</h3>
<p>Lorem ipsum dolor sit amet</p>
<button id="exit">Close Dialog</button>
</dialog>
<button id="show">Show Dialog</button>
JavaScript:-
(function() {
var dialog = document.getElementById('window');
document.getElementById('show').onclick = function() {
dialog.show();
};
document.getElementById('exit').onclick = function() {
dialog.close();
};
})();
-
I get a `TypeError: dialog.show is not a function` error. Could you please inlude a JSFiddle? – Magiranu Nov 03 '17 at 10:33
Here is a very simple popup:
<!DOCTYPE html>
<html>
<head>
<style>
#modal {
position:absolute;
background:gray;
padding:8px;
}
#content {
background:white;
padding:20px;
}
#close {
position:absolute;
background:url(close.png);
width:24px;
height:27px;
top:-7px;
right:-7px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var modal = (function(){
// Generate the HTML and add it to the document
$modal = $('<div id="modal"></div>');
$content = $('<div id="content"></div>');
$close = $('<a id="close" href="#"></a>');
$modal.hide();
$modal.append($content, $close);
$(document).ready(function(){
$('body').append($modal);
});
$close.click(function(e){
e.preventDefault();
$modal.hide();
$content.empty();
});
// Open the modal
return function (content) {
$content.html(content);
// Center the modal in the viewport
$modal.css({
top: ($(window).height() - $modal.outerHeight()) / 2,
left: ($(window).width() - $modal.outerWidth()) / 2
});
$modal.show();
};
}());
// Wait until the DOM has loaded before querying the document
$(document).ready(function(){
$('a#popup').click(function(e){
modal("<p>This is popup's content.</p>");
e.preventDefault();
});
});
</script>
</head>
<body>
<a id='popup' href='#'>Simple popup</a>
</body>
</html>
More flexible solution can be found in this tutorial: http://www.jacklmoore.com/notes/jquery-modal-tutorial/ Here's close.png for the sample.

- 3,340
- 3
- 32
- 50
ONLY CSS POPUP LOGIC! TRY DO IT . EASY! I think this mybe be hack popular in future
<a href="#openModal">OPEN</a>
<div id="openModal" class="modalDialog">
<div>
<a href="#close" class="close">X</a>
<h2>MODAL</h2>
</div>
</div>
.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;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
display: none;
pointer-events: none;
}
.modalDialog:target {
display: block;
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);
}

- 10,592
- 8
- 70
- 89