Hide or show a <p> element when a button is clicked:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"> </script>/*from w w w .j a v a2s.co m*/ <script> $(document).ready(function(){ $("button").live("click", function(){ $("p").slideToggle(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button>Click me!</button> </body> </html>
The live()
method was deprecated in jQuery version 1.7, and removed in version 1.9.
Use the on()
method instead.
The live()
method attaches one or more event handlers for selected elements.
The live()
method runs a function when the events occur.
Event handlers attached using the live()
method will work for both current and future elements.
To remove event handlers, use the die()
method.
$(selector).live(event,data,function)
Parameter | Optional | Description |
---|---|---|
event | Required. | one or more events to attach to the elements. Multiple event values are separated by space. |
data | Optional. | additional data to pass to the function |
function | Required. | the function to run when the event occurs |