jQuery events
jQuery is specially designed for event handling.
What is an event?
A page's response to different visitors is called an event.
Event handlers are methods that are called when certain events occur in HTML.
Example:
- Move the mouse over an element.
- Select a radio button
- Click an element
The term "trigger" (or "fire") is often used with events, e.g.: "the keypress event is triggered when you press a key".
Common DOM events:
| Mouse Events | Keyboard Events | Form Events | Document/window events |
|---|---|---|---|
| click | keypress | submit | load |
| dblclick | keydown | change | resize |
| mouseenter | keyup | focus | scroll |
| mouseleave | blur | unload | |
| hover |
jQuery event method syntax
In jQuery, most DOM events have an equivalent jQuery method.
Specify a click event on a page:
$
(
"
p
"
)
.
click
(
)
;
The next step is to define what happens when the click triggers the event. You can do this with an event function:
$
(
"
p
"
)
.
click
(
function
(
)
{
//
Code to execute when the action is triggered!!
}
)
;
Commonly used jQuery event methods
$(document).ready()
The $(document).ready() method lets us execute a function when the document is fully loaded. This event method was mentioned in the jQuery syntax chapter.
click()
The click() method calls a function when a button click event is triggered.
This function executes when the user clicks an HTML element.
In the example below, when a click event is triggered on a
element, hide the current
element:
Example
$
(
"
p
"
)
.
click
(
function
(
)
{
$
(
this
)
.
hide
(
)
;
}
)
;
Try it »
dblclick()
The dblclick event occurs when an element is double-clicked.
The dblclick() method triggers the dblclick event, or specifies a function to run when a dblclick event occurs:
Example
$
(
"
p
"
)
.
dblclick
(
function
(
)
{
$
(
this
)
.
hide
(
)
;
}
)
;
Try it »
mouseenter()
The mouseenter event occurs when the mouse pointer enters an element.
The mouseenter() method triggers the mouseenter event, or specifies a function to run when a mouseenter event occurs:
Example
$
(
"
#p1
"
)
.
mouseenter
(
function
(
)
{
alert
(
'
Your mouse moved over the element with id="p1"!
'
)
;
}
)
;
Try it »
mouseleave()
The mouseleave event occurs when the mouse pointer leaves an element.
The mouseleave() method triggers the mouseleave event, or specifies a function to run when a mouseleave event occurs:
Example
$
(
"
#p1
"
)
.
mouseleave
(
function
(
)
{
alert
(
"
Bye, your mouse left the paragraph.
"
)
;
}
)
;
Try it »
mousedown()
The mousedown event occurs when the mouse pointer moves over an element and a mouse button is pressed.
The mousedown() method triggers the mousedown event, or specifies a function to run when a mousedown event occurs:
Example
$
(
"
#p1
"
)
.
mousedown
(
function
(
)
{
alert
(
"
Mouse pressed down on the paragraph!
"
)
;
}
)
;
Try it »
mouseup()
The mouseup event occurs when a mouse button is released over an element.
The mouseup() method triggers the mouseup event, or specifies a function to run when a mouseup event occurs:
Example
$
(
"
#p1
"
)
.
mouseup
(
function
(
)
{
alert
(
"
Mouse released over the paragraph.
"
)
;
}
)
;
Try it »
hover()
The hover() method is used to simulate cursor hovering events.
When the mouse moves over an element, the specified first function (mouseenter) is triggered; when the mouse moves out of the element, the specified second function (mouseleave) is triggered.
Example
$
(
"
#p1
"
)
.
hover
(
function
(
)
{
alert
(
"
You entered p1!
"
)
;
}
,
function
(
)
{
alert
(
"
Bye bye! Now you left p1!
"
)
;
}
)
;
Try it »
focus()
The focus event occurs when an element receives focus.
An element receives focus when it is selected by a mouse click or located with the tab key.
The focus() method triggers the focus event, or specifies a function to run when a focus event occurs:
Example
$
(
"
input
"
)
.
focus
(
function
(
)
{
$
(
this
)
.
css
(
"
background-color
"
,
"
#cccccc
"
)
;
}
)
;
Try it »
blur()
The blur event occurs when an element loses focus.
The blur() method triggers the blur event, or specifies a function to run when a blur event occurs:
Example
$
(
"
input
"
)
.
blur
(
function
(
)
{
$
(
this
)
.
css
(
"
background-color
"
,
"
#ffffff
"
)
;
}
)
;
Try it »