jQuery One Event

Monday, July 02, 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.


Syntax

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


ParameterDescription
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


Example

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 ....
         </div>
</div>

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


mouse
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

JavaScript and jQuery: The Missing Manual Professional jQuery HTML CSS and JavaScript Editor