How to generate a simple popup using jQuery

How to generate a simple popup using jQuery

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?


Solution 1:

First the CSS – tweak this however you like:

a.selected {

.messagepop {
  border:1px solid #999999;
  margin-top: 15px;
  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() {

$(function() {
  $('#contact').on('click', function() {
    if($(this).hasClass('selected')) {
    } else {
    return false;

  $('.close').on('click', function() {
    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>

<a href="/contact" id="contact">Contact Us</a>

Here is a jsfiddle demo and implementation.

Related:  Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop

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 class="messagepop pop"></div> 
    <a href="/contact" id="contact">Contact Us</a>

And the general idea of the JavaScript becomes:

$("#contact").on('click', function() {
    if($(this).hasClass("selected")) {
    } else {
        $.get(this.href, function(data) {
            $(".pop").html(data).slideFadeToggle(function() { 
    return false;

Solution 2:

Check out jQuery UI Dialog. You would use it like this:

The jQuery:

$(document).ready(function() {

The markup:

<div id="dialog" title="Dialog Title">I'm in a dialog</div>


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.

Solution 3:

I use a jQuery plugin called ColorBox, it is

  1. Very easy to use
  2. lightweight
  3. customizable
  4. the nicest popup dialog I have seen for jQuery yet
Related:  What is the value of var me = this;

Solution 4:

Visit this url

Jquery UI Dialog Demos

Solution 5:

Try the Magnific Popup, it’s responsive and weights just around 3KB.

Solution 6:

I think this is a great tutorial on writing a simple jquery popup. Plus it looks very beautiful