.one( eventType, [ eventData ], handler(eventObject) ) Returns: jQuery

Description: Attach a handler to an event for the elements. The handler is executed at most once.

  • version added: 1.1.one( eventType, [ eventData ], handler(eventObject) )

    eventTypeA string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute at the time the event is triggered.

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

$('#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:

$('#foo').bind('click', function(event) {
  alert('This will be displayed only once.');

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


Example: Tie a one-time click to each div.

<!DOCTYPE html>
div { width:60px; height:60px; margin:5px; float:left;
background:green; border:10px outset; 
cursor:pointer; }
p { color:red; margin:0; clear:left; }
  <script src="http://code.jquery.com/jquery-latest.js"></script>

<p>Click a green square...</p>
var n = 0;
$("div").one("click", function(){
var index = $("div").index(this);
$(this).css({ borderStyle:"inset",
    cursor:"auto" });
$("p").text("Div at index #" + index + " clicked." +
  "  That's " + ++n + " total clicks.");



Example: To display the text of all paragraphs in an alert box the first time each of them is clicked:

$("p").one("click", function(){
alert( $(this).text() );