jQuery One Event

Monday, July 2, 2012

The jQuery one event is used to attach one or more event handlers for selected elements. It also specifies a function to run when the event is triggered. When using the one method, the event handler is only triggered once per attached element.



eventOne or more events to attach. Event values are separated by space
dataAdditional optional data to pass along to the function
functionOptional function to run when the event(s) occur


In the following jQuery example, the one() method adds the click event to the element with an id of "imgMouse". When the "imgMouse" element is clicked, the "click" event is executed through the one method. In this example, the div text is altered and the font-size is changed as the mouse icon is clicked. Try it yourself, by clicking the mouse icon.

<div id="divParent">
    <img id="imgMouse"src="#" />
    <div id="divChild">
    <div id="divText">Click the mouse icon ....

$("#imgMouse").one("click", function () {
    $("#divChild").css("font-size", "2.5em");
    $("#divChild").text("Thanks for clicking me!");

Click the mouse icon so I can grow in size!

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Professional jQuery Pro jQuery JavaScript and jQuery: The Missing Manual