Created
May 15, 2014 11:05
-
-
Save carlosrojaso/78bb23615cc23c55ba11 to your computer and use it in GitHub Desktop.
JQ events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The many ways to bind events with jQuery | |
// Attach an event handler directly to the button using jQuery's | |
// shorthand `click` method. | |
$( "#helloBtn" ).click(function( event ) { | |
alert( "Hello." ); | |
}); | |
// Attach an event handler directly to the button using jQuery's | |
// `bind` method, passing it an event string of `click` | |
$( "#helloBtn" ).bind( "click", function( event ) { | |
alert( "Hello." ); | |
}); | |
// As of jQuery 1.7, attach an event handler directly to the button | |
// using jQuery's `on` method. | |
$( "#helloBtn" ).on( "click", function( event ) { | |
alert( "Hello." ); | |
}); | |
// As of jQuery 1.7, attach an event handler to the `body` element that | |
// is listening for clicks, and will respond whenever *any* button is | |
// clicked on the page. | |
$( "body" ).on({ | |
click: function( event ) { | |
alert( "Hello." ); | |
} | |
}, "button" ); | |
// An alternative to the previous example, using slightly different syntax. | |
$( "body" ).on( "click", "button", function( event ) { | |
alert( "Hello." ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment