Skip to content

Instantly share code, notes, and snippets.

@lishiyo
Last active August 29, 2015 14:03
Show Gist options
  • Save lishiyo/95798b62450c74364b59 to your computer and use it in GitHub Desktop.
Save lishiyo/95798b62450c74364b59 to your computer and use it in GitHub Desktop.
Blog - jQuery events
//calls handler once and sets up subsequent action
$( "button" ).one( "click", firstClick );
function firstClick() {
$('p').text( "You just clicked this for the first time!" );
// Set up the new handler for subsequent clicks with $(this).click
// (omit if no further click responses are needed)
$( this ).click( function(){
$('p').text( "You\'ve clicked this before!" );
});
}
// You can also .one() to bind several events - each will be allowed to be called once
$( "input[id]" ).one( "focus mouseover keydown", firstEvent);
function firstEvent( eventObject ) {
console.log( "A " + eventObject.type + " event occurred for the first time on the input with id " + this.id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment