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

Recommended Posts:
- jQuery | before() Method
- jQuery | add() method with Example
- jQuery | die() Method
- jQuery | off() Method
- jQuery | is() Method
- jQuery | get() Method
- jQuery | error() Method
- jQuery | Misc get() Method
- jQuery | trigger() Method
- jQuery | ajaxError() Method
- jQuery | getScript() Method
- jQuery | unbind() Method
- jQuery | append() Method
- jQuery | ajaxComplete() Method
- jQuery | text() Method
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.



