English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery one() Method

jQuery Events

The one() method attaches one or more event handlers to the selected elements.

also attaches the function to be executed when the event occurs.

This method is named one because any event handler attached to this method for each elementwill only run once.

Syntax:

$(selector).one(event, data, function)

Example

Attach a click event to all <p> elements (the event will only trigger once for each <p> element):

$("p").one("click", function(){
  $(this).animate({fontSize: "+=5px"});
});
Test to see‹/›

Display the difference between the one() and on() methods:

$(document).ready(function() {
  $("#para-1").one("click", myFunc);
  $("#para-2.on("click", myFunc);
});
Test to see‹/›

Add multiple event handlers to all <p> elements:

$("p").one("click dblclick", function() {
  $(this).animate({fontSize: "+=5px"});
});
Test to see‹/›

Pass data to the function:

$(document).ready(function() {
  $("p").one("click", {msg: "You just clicked me!!!"}, showMsg)
});
function showMsg(event) {
  $(this).append(event.data.msg);
});
Test to see‹/›

Parameter Value

ParameterDescription
eventSpecify one or more events separated by spaces
data(Optional) Specify other data to be passed to the function
Note:IfDataParameters provided to the one() method are passed toevent.dataHandler in the property.
functionFunctionality executed when the event is triggered

jQuery Events