Open In App

How to create a pop-up div on mouse over and stay when click using jQuery ?

In this article, we will learn how to create a pop-up div on mouseover and stay when click using jQuery.

Approach: 



display:none;
 $flag = -1;

JavaScript Code:

$("a.gfg").hover(
    function () {
        $("div.popup").attr("style", "display:block");
    },
    function () {
        if ($flag == -1) {
            $("div.popup").attr("style", "display:none");
        }
    }
);
$("a.gfg").click(function () {
    $flag = 1;
});

HTML Code: Below is the full implementation of the above approach.






<!DOCTYPE html>
<html>
  
<head>
  
    <!-- JQuery CDN -->
    <script src=
    </script>
  
    <style>
        center {
            font-size: 30px;
            color: green;
        }
  
        .popup {
            display: none;
            width: 500px;
            border: solid red 3px
        }
    </style>
</head>
  
<body>
    <center>
        <p>
            Hover <a href="#" class="gfg">here</a
            to see the changes.
        </p>
  
        <div class="popup">
            GeeksforGeeks
        </div>
    </center>
  
    <script>
        $flag = -1;
  
        $("a.gfg").hover(
            function () {
                $("div.popup").attr("style", "display:block");
            },
            function () {
                if ($flag == -1) {
                    $("div.popup").attr("style", "display:none");
                }
            }
        );
  
        $("a.gfg").click(function () {
            $flag = 1;
        });
    </script>
</body>
  
</html>

Output:

pop up mouse hover


Article Tags :