.click()
JQ Home <<
Events <<
.click()
Mouse click event handler.
Description
The .click()
method is used to bind an event handler to the JavaScript click
event or trigger that event on the specified element.
- The
click
event is sent to an element when:- The mouse pointer is inside the element and the primary mouse button is pressed down.
- The primary mouse button is then released while the mouse pointer is inside the element.
- For situations where you just need to detect when a mouse buton is pressed it is preferable to use the
.keydown()
- For situations where you just need to detect when a mouse buton is released it is preferable to use the
.keyup()
Syntax
Signature | Description |
---|---|
.click( ) | Trigger the click JavaScript event on the specified element. |
.click( handler(eventObject) ) | Bind an event handler to the click JavaScript event. |
.click( [eventData ,] handler(eventObject) ) | Bind an event handler to the click JavaScript event, optionally passing an object of data. |
Parameters
Parameter | Description | Type |
---|---|---|
handler( eventObject ) | A function to execute each time the event is triggered. | Function |
eventData | An object of data to pass to the event handler. | Anything |
Return
A jQuery
object.
.click( )
Example
Events << Top
Trigger the click
JavaScript event on the specified element.
- This signature is a shortcut for
.trigger('click')
.
In the example below we toggle the pie image with the id of 'pie1' when the user clicks anywhere in the div with an id of 'div1' or clicks the button below.
When the user clicks the button we trigger off the click
JavaScript event on the 'div14' element. This then fires off the $('#div1').click(function(){})
code which shows the image.
Alternatively if you just click in the division the click
event handler runs without being triggered to toggle the image.
$(function(){
$('#btn1').click(function () {
$('#div1').click();
});
$('#div1').click(function () {
$('#pie1').toggle();
});
});
.click( handler(eventObject) )
Example
Events << Top
Bind an event handler to the click
JavaScript event.
- This signature is a shortcut for
.on('click', handler)
.
In the example below we show a new message in the 'p' element with an id of 'scrollspan1' each time the 'div' element with an id of 'div2' below is hovered over and the primary mouse button is pressed and released inside this element.
When the primary mouse button is pressed and released inside the element, the click
JavaScript event fires off the addText(event)
mothod which outputs a message.
What we are doing here is passing across the event
object to the function addText(event)
method. The data we specify gets tagged onto the event.data
property.
$(function(){
$('#div2').click(addText);
function addText(event) {
$('#scrollspan1').append('click 1 **JavaScript event triggered**<br>');
}
});
We will show a message here.
.click( [eventData ,] handler(eventObject) )
Example
Events << Top
Bind an event handler to the click
JavaScript event, optionally passing an object of data.
- This signature is a shortcut for
.on('click', handler)
.
In the example below we show a new message in the 'p' element with an id of 'scrollspan2' each time the 'div' element with an id of 'div3' below is hovered over and the primary mouse button is pressed and released inside this element.
When the primary mouse button is pressed and released inside the element, the click
JavaScript event fires off the $('#div3').click({ param1: '#scrollspan2', param2: 'click 2 ', param3: '**JavaScript event triggered** ' }, addText2);
code.
What we are doing here is passing across the event
object to the function addText2(event)
. The map we specify, in our case { param1: '#scrollspan2', param2: 'click 2 ', param3: '**JavaScript event triggered** ' }
gets tagged onto the event.data
property. We then access this parameter in the function via event.data.param
and use it as part of the appended data.
$(function(){
$('#div3').click({ param1: '#scrollspan2', param2: 'click 2 ',
param3: '**JavaScript event triggered** ' }, addText2);
function addText2(event) {
$(event.data.param1).append( event.data.param2 + event.data.param3);
}
});
We will show a message here.
Related Tutorials
jQuery Advanced Tutorials - Lesson 2 - Keyboard & Mouse Events