Skip to content
Related Articles

Related Articles

Improve Article

jQuery | live() Method

  • Last Updated : 27 Feb, 2019

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :