jQuery website jQuery documentation

Events

bind(String type, Object data, Function fn) : jQuery

typeStringAn event type
dataObject(optional) Additional data passed to the event handler as event.data
fnFunctionA function to bind to the event on each of the set of matched elements

Binds a handler to a particular event (like click) for each matched element. The event handler is passed an event object that you can use to prevent default behaviour. To stop both default action and event bubbling, your handler has to return false.

In most cases, you can define your event handlers as anonymous functions (see first example). In cases where that is not possible, you can pass additional data as the second paramter (and the handler function as the third), see second example.

Examples

Before
<p>Hello</p>
Code
$("p").bind("click", function(){
  alert( $(this).text() );
});
Result
alert("Hello")

Pass some additional data to the event handler.

Code
function handler(event) {
  alert(event.data.foo);
}
$("p").bind("click", {foo: "bar"}, handler)
Result
alert("bar")

Cancel a default action and prevent it from bubbling by returning false from your function.

Code
$("form").bind("submit", function() { return false; })

Cancel only the default action by using the preventDefault method.

Code
$("form").bind("submit", function(event){
  event.preventDefault();
});

Stop only an event from bubbling by using the stopPropagation method.

Code
$("form").bind("submit", function(event){
  event.stopPropagation();
});

one(String type, Object data, Function fn) : jQuery

typeStringAn event type
dataObject(optional) Additional data passed to the event handler as event.data
fnFunctionA function to bind to the event on each of the set of matched elements

Binds a handler to a particular event (like click) for each matched element. The handler is executed only once for each element. Otherwise, the same rules as described in bind() apply. The event handler is passed an event object that you can use to prevent default behaviour. To stop both default action and event bubbling, your handler has to return false.

In most cases, you can define your event handlers as anonymous functions (see first example). In cases where that is not possible, you can pass additional data as the second paramter (and the handler function as the third), see second example.

Example

Before
<p>Hello</p>
Code
$("p").one("click", function(){
  alert( $(this).text() );
});
Result
alert("Hello")

unbind(String type, Function fn) : jQuery

typeString(optional) An event type
fnFunction(optional) A function to unbind from the event on each of the set of matched elements

The opposite of bind, removes a bound event from each of the matched elements.

Without any arguments, all bound events are removed.

If the type is provided, all bound events of that type are removed.

If the function that was passed to bind is provided as the second argument, only that specific event handler is removed.

Examples

Before
<p onclick="alert('Hello');">Hello</p>
Code
$("p").unbind()
Result
[ <p>Hello</p> ]
Before
<p onclick="alert('Hello');">Hello</p>
Code
$("p").unbind( "click" )
Result
[ <p>Hello</p> ]
Before
<p onclick="alert('Hello');">Hello</p>
Code
$("p").unbind( "click", function() { alert("Hello"); } )
Result
[ <p>Hello</p> ]

trigger(String type) : jQuery

typeStringAn event type to trigger.

Trigger a type of event on every matched element.

Example

Before
<p click="alert('hello')">Hello</p>
Code
$("p").trigger("click")
Result
alert('hello')

toggle(Function even, Function odd) : jQuery

evenFunctionThe function to execute on every even click.
oddFunctionThe function to execute on every odd click.

Toggle between two function calls every other click. Whenever a matched element is clicked, the first specified function is fired, when clicked again, the second is fired. All subsequent clicks continue to rotate through the two functions.

Use unbind("click") to remove.

Example

Code
$("p").toggle(function(){
  $(this).addClass("selected");
},function(){
  $(this).removeClass("selected");
});

hover(Function over, Function out) : jQuery

overFunctionThe function to fire whenever the mouse is moved over a matched element.
outFunctionThe function to fire whenever the mouse is moved off of a matched element.

A method for simulating hovering (moving the mouse on, and off, an object). This is a custom method which provides an 'in' to a frequent task.

Whenever the mouse cursor is moved over a matched element, the first specified function is fired. Whenever the mouse moves off of the element, the second specified function fires. Additionally, checks are in place to see if the mouse is still within the specified element itself (for example, an image inside of a div), and if it is, it will continue to 'hover', and not move out (a common error in using a mouseout event handler).

Example

Code
$("p").hover(function(){
  $(this).addClass("over");
},function(){
  $(this).addClass("out");
});

ready(Function fn) : jQuery

fnFunctionThe function to be executed when the DOM is ready.

Bind a function to be executed whenever the DOM is ready to be traversed and manipulated. This is probably the most important function included in the event module, as it can greatly improve the response times of your web applications.

In a nutshell, this is a solid replacement for using window.onload, and attaching a function to that. By using this method, your bound Function will be called the instant the DOM is ready to be read and manipulated, which is exactly what 99.99% of all Javascript code needs to run.

There is one argument passed to the ready event handler: A reference to the jQuery function. You can name that argument whatever you like, and can therefore stick with the $ alias without risc of naming collisions.

Please ensure you have no code in your onload event handler, otherwise $(document).ready() may not fire.

You can have as many $(document).ready events on your page as you like. The functions are then executed in the order they were added.

Examples

Code
$(document).ready(function(){ Your code here... });

Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.

Code
jQuery(function($) {
  // Your code using failsafe $ alias here...
});

scroll(Function fn) : jQuery

fnFunctionA function to bind to the scroll event on each of the matched elements.

Bind a function to the scroll event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").scroll( function() { alert("Hello"); } );
Result
<p onscroll="alert('Hello');">Hello</p>

submit(Function fn) : jQuery

fnFunctionA function to bind to the submit event on each of the matched elements.

Bind a function to the submit event of each matched element.

Example

Prevents the form submission when the input has no value entered.

Before
<form id="myform"><input /></form>
Code
$("#myform").submit( function() {
  return $("input", this).val().length > 0;
} );

submit() : jQuery

Trigger the submit event of each matched element. This causes all of the functions that have been bound to thet submit event to be executed.

Note: This does not execute the submit method of the form element! If you need to submit the form via code, you have to use the DOM method, eg. $("form")[0].submit();

Example

Triggers all submit events registered for forms, but does not submit the form

Code
$("form").submit();

focus(Function fn) : jQuery

fnFunctionA function to bind to the focus event on each of the matched elements.

Bind a function to the focus event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").focus( function() { alert("Hello"); } );
Result
<p onfocus="alert('Hello');">Hello</p>

focus() : jQuery

Trigger the focus event of each matched element. This causes all of the functions that have been bound to thet focus event to be executed.

Note: This does not execute the focus method of the underlying elements! If you need to focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus();

Example

Before
<p onfocus="alert('Hello');">Hello</p>
Code
$("p").focus();
Result
alert('Hello');

keydown(Function fn) : jQuery

fnFunctionA function to bind to the keydown event on each of the matched elements.

Bind a function to the keydown event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").keydown( function() { alert("Hello"); } );
Result
<p onkeydown="alert('Hello');">Hello</p>

dblclick(Function fn) : jQuery

fnFunctionA function to bind to the dblclick event on each of the matched elements.

Bind a function to the dblclick event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").dblclick( function() { alert("Hello"); } );
Result
<p ondblclick="alert('Hello');">Hello</p>

keypress(Function fn) : jQuery

fnFunctionA function to bind to the keypress event on each of the matched elements.

Bind a function to the keypress event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").keypress( function() { alert("Hello"); } );
Result
<p onkeypress="alert('Hello');">Hello</p>

error(Function fn) : jQuery

fnFunctionA function to bind to the error event on each of the matched elements.

Bind a function to the error event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").error( function() { alert("Hello"); } );
Result
<p onerror="alert('Hello');">Hello</p>

blur(Function fn) : jQuery

fnFunctionA function to bind to the blur event on each of the matched elements.

Bind a function to the blur event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").blur( function() { alert("Hello"); } );
Result
<p onblur="alert('Hello');">Hello</p>

blur() : jQuery

Trigger the blur event of each matched element. This causes all of the functions that have been bound to thet blur event to be executed.

Note: This does not execute the blur method of the underlying elements! If you need to blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur();

Example

Before
<p onblur="alert('Hello');">Hello</p>
Code
$("p").blur();
Result
alert('Hello');

load(Function fn) : jQuery

fnFunctionA function to bind to the load event on each of the matched elements.

Bind a function to the load event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").load( function() { alert("Hello"); } );
Result
<p onload="alert('Hello');">Hello</p>

select(Function fn) : jQuery

fnFunctionA function to bind to the select event on each of the matched elements.

Bind a function to the select event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").select( function() { alert("Hello"); } );
Result
<p onselect="alert('Hello');">Hello</p>

select() : jQuery

Trigger the select event of each matched element. This causes all of the functions that have been bound to thet select event to be executed.

Example

Before
<p onselect="alert('Hello');">Hello</p>
Code
$("p").select();
Result
alert('Hello');

mouseup(Function fn) : jQuery

fnFunctionA function to bind to the mouseup event on each of the matched elements.

Bind a function to the mouseup event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").mouseup( function() { alert("Hello"); } );
Result
<p onmouseup="alert('Hello');">Hello</p>

unload(Function fn) : jQuery

fnFunctionA function to bind to the unload event on each of the matched elements.

Bind a function to the unload event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").unload( function() { alert("Hello"); } );
Result
<p onunload="alert('Hello');">Hello</p>

change(Function fn) : jQuery

fnFunctionA function to bind to the change event on each of the matched elements.

Bind a function to the change event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").change( function() { alert("Hello"); } );
Result
<p onchange="alert('Hello');">Hello</p>

mouseout(Function fn) : jQuery

fnFunctionA function to bind to the mouseout event on each of the matched elements.

Bind a function to the mouseout event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").mouseout( function() { alert("Hello"); } );
Result
<p onmouseout="alert('Hello');">Hello</p>

keyup(Function fn) : jQuery

fnFunctionA function to bind to the keyup event on each of the matched elements.

Bind a function to the keyup event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").keyup( function() { alert("Hello"); } );
Result
<p onkeyup="alert('Hello');">Hello</p>

click(Function fn) : jQuery

fnFunctionA function to bind to the click event on each of the matched elements.

Bind a function to the click event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").click( function() { alert("Hello"); } );
Result
<p onclick="alert('Hello');">Hello</p>

click() : jQuery

Trigger the click event of each matched element. This causes all of the functions that have been bound to thet click event to be executed.

Example

Before
<p onclick="alert('Hello');">Hello</p>
Code
$("p").click();
Result
alert('Hello');

resize(Function fn) : jQuery

fnFunctionA function to bind to the resize event on each of the matched elements.

Bind a function to the resize event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").resize( function() { alert("Hello"); } );
Result
<p onresize="alert('Hello');">Hello</p>

mousemove(Function fn) : jQuery

fnFunctionA function to bind to the mousemove event on each of the matched elements.

Bind a function to the mousemove event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").mousemove( function() { alert("Hello"); } );
Result
<p onmousemove="alert('Hello');">Hello</p>

mousedown(Function fn) : jQuery

fnFunctionA function to bind to the mousedown event on each of the matched elements.

Bind a function to the mousedown event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").mousedown( function() { alert("Hello"); } );
Result
<p onmousedown="alert('Hello');">Hello</p>

mouseover(Function fn) : jQuery

fnFunctionA function to bind to the mousedown event on each of the matched elements.

Bind a function to the mouseover event of each matched element.

Example

Before
<p>Hello</p>
Code
$("p").mouseover( function() { alert("Hello"); } );
Result
<p onmouseover="alert('Hello');">Hello</p>