A repository of over 1000 quality jQuery plugins

jQuery .one()

Learn all about the jQuery function .one().

The .one() method is identical to .on(), except that the handler is unbound after its first invocation. For example:

1
2
3
$( "#foo" ).one( "click", function() {
alert( "This will be displayed only once." );
});

After the code is executed, a click on the element with ID foo will display the alert. Subsequent clicks will do nothing. This code is equivalent to:

1
2
3
4
$( "#foo" ).on( "click", function( event ) {
alert( "This will be displayed only once." );
$( this ).off( event );
});

In other words, explicitly calling .off() from within a regularly-bound handler has exactly the same effect.

If the first argument contains more than one space-separated event types, the event handler is called once for each event type.