.one()

Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

.one(events, data, function(eventObjectEvent))🡢 jQuery

events StringA string containing one or more JavaScript event types, such as "click" or "submit," or custom event names.
data PlainObjectData to be passed to the handler in event.data when an event is triggered.
function(eventObjectEvent) FunctionA function to execute at the time the event is triggered.

.one(events, selector, data, function(eventObjectEvent))🡢 jQuery

events StringOne or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin".
selector StringA selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.
data AnythingData to be passed to the handler in event.data when an event is triggered.
function(eventObjectEvent) FunctionA function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false.

.one(events, selector, data)🡢 jQuery

events PlainObjectAn object in which the string keys represent one or more space-separated event types and optional namespaces, and the values represent a handler function to be called for the event(s).
selector StringA selector string to filter the descendants of the selected elements that will call the handler. If the selector is null or omitted, the handler is always called when it reaches the selected element.
data AnythingData to be passed to the handler in event.data when an event occurs.

The .one() method is identical to .on(), except that the handler for a given element and event type 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").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.

$("#foo").one("click mouseover", function (event) {
  alert("The " + event.type + " event happened!");
});

In the example above the alert could be displayed twice due to the two event types (click and mouseover).

Tie a one-time click to each div.

JS
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<p>Click a green square...</p>
CSS
div {
  width: 60px;
  height: 60px;
  margin: 5px;
  float: left;
  background: green;
  border: 10px outset;
  cursor: pointer;
}
p {
  color: red;
  margin: 0;
  clear: left;
}
HTML
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."
  );
});
DEMO

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

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

Event handlers will trigger once per element per event type

JS
<div class="count">0</div>
<div class="target">Hover/click me</div>
HTML
var n = 0;
$(".target").one("click mouseenter", function () {
  $(".count").html(++n);
});
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)