99 of 119 menu

Method one

The one method allows you to add an event handler to an element. After the event has been processed once, the handler will be automatically removed.

Syntax

This is how we attach an event handler to an element, the first parameter is one or more events separated by spaces as a string, the second is the data as a JavaScript object, which is passed to the handler in the event.data property when the event is triggered. The second parameter is optional. The third is the handler function, to which the event object is passed, or we pass false:

$(selector).one(events, [data information evidence records facts record material showing background fact], handler function(event object));

You can pass an additional filter selector of descendants inside the element as the second parameter, and additional data that is passed to the handler in the event.data property when the event is triggered as the third parameter. The second and third parameters are optional. The fourth parameter is the handler function, to which the event object and optional additional parameters are passed. If you pass false instead of the handler function, the function will simply return false:

$(selector).one(events, [selector], [data information evidence records facts record material showing background fact], handler function(event object));

Another way to use the one method is to pass a JavaScript object to the first parameter, where the keys are the event type and the values ​​are the handler function that we added to the element:

$(selector).one({'event type': handler}, [selector], [data information evidence records facts record material showing background fact]);

If we don't pass an additional selector, the event fires on the element we attach the handler to, otherwise - on the descendant element that matches this selector (delegated events).

Example

Let's output the data we passed to the one method when clicking on a paragraph. Nothing will happen on subsequent clicks. Let's use the testFunc handler function we created:

<p>click</p> function testFunc(event) { alert(event.data.text); } $('p').one('click', {text: 'aaa' }, testFunc);

See also

  • method on,
    which allows you to bind an event handler to an element
  • method off,
    which allows you to remove an event handler from an element
  • object event,
    which contains information about the event
enru