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:

  • Event: It is used to specify events, which will be attached to the elements. If there are more than one event, then they are separated by space.
  • Function: It is used to specify the function, which will run when the event will occurs.
  • data: It is used to specify additional data pass with the function. It is an optional property.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

Before clicking on the button:

After clicking on the button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

Initially:

Before clicking on the button:

After clicking on the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.