Open In App
Related Articles

How to generate a simple popup using jQuery ?

Improve Article
Save Article
Like Article

The task is to generate a popup using jQuery. Popups are useful to splash important information to the visitors of a website.

Approach: A simple pop can be made by using toggle() method of jQuery which toggles between hide() and show() function of jQuery i.e. it checks the visibility of the selector used. The hide() method is run when the selector is visible and show() is run when the selector is not visible.

It displays the popup if the show popup button is clicked and hides the popup if the closed button is clicked.

The selector used for toggling is ” .content ” which contains the close button and popup’s body. Initially to hide the popup on page reload we have used the display: none property on the .content class in style tag. 

Now when the user clicks on the show popup button the onclick event calls the togglePopup() which displays the popup and when the user clicks on the close button onclick event again calls the togglePopup() which hides the popup.





<!DOCTYPE html>
    <!-- jQuery cdn link -->
    <script src=
    <style type="text/css">
        .content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 500px;
            height: 200px;
            text-align: center;
            background-color: #e8eae6;
            box-sizing: border-box;
            padding: 10px;
            z-index: 100;
            display: none;
            /*to hide popup initially*/
        .close-btn {
            position: absolute;
            right: 20px;
            top: 15px;
            background-color: black;
            color: white;
            border-radius: 50%;
            padding: 4px;
    <button onclick="togglePopup()">show popup</button>
    <!-- div containing the popup -->
    <div class="content">
        <div onclick="togglePopup()" class="close-btn">
            jQuery is an open source JavaScript library 
            that simplifies the interactions between an
            HTML/CSS document, or more precisely the 
            Document Object Model (DOM), and JavaScript.
            Elaborating the terms, jQuery simplifies 
            HTML document traversing and manipulation, 
            browser event handling, DOM animations, 
            Ajax interactions, and cross-browser 
            JavaScript development.
    <script type="text/javascript">
        // Function to show and hide the popup
        function togglePopup() {


Last Updated : 27 Jan, 2021
Like Article
Save Article
Similar Reads
Related Tutorials