Skip to content

Instantly share code, notes, and snippets.

@carlosrojaso
Created May 15, 2014 11:05
Show Gist options
  • Save carlosrojaso/78bb23615cc23c55ba11 to your computer and use it in GitHub Desktop.
Save carlosrojaso/78bb23615cc23c55ba11 to your computer and use it in GitHub Desktop.
JQ events
// 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