Open In App

jQuery | live() Method

This method is used to attach one or more event handlers for the selected elements. It also specifies the function that runs when the event occurs. The event handlers used will work for both current and future elements matching the selector.

Syntax:



$(selector).live(event, data, function)

Property Values:

Example-1: Display and hide text when event occurs.




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    <script>
        $(document).ready(function() {
            $("button").live("click", function() {
                $("p").slideToggle();
            });
        });
    </script>
</head>
  
<body>
  
    <p>Geeks for Geeks</p>
  
    <button>Press</button>
    <br>
    <br>
  
    <div><b><h4>Clicking on the 'Press' 
      button will execute the live method().
      </h4></b> </div>
</body>
  
</html>

Output:



Before clicking on the button:

After clicking on the button:

Example-2: Insert element and hide when event occurs.




<!DOCTYPE html>
<html>
  
<body>
    <h1><center>Geeks
      </center>
  </h1>
    <script src=
    </script>
    <script>
        $(document).ready(function() {
            
            $("p").live("click", function() {
                $(this).slideToggle();
            });
            $("button").click(function() {
                $("<p>Inserted Element</p>").insertAfter(
                  "button");
            });
        });
    </script>
    <p>Click here to make it disappear.</p>
    <button>Click to insert an element</button>
  
</body>
  
</html>

Output:

Initially:

Before clicking on the button:

After clicking on the button:


Article Tags :