.triggerHandler()
S2C Home « Events « .triggerHandler()
Event handler.
Description
The .triggerHandler()
method is used to manually fire all event handlers on an element, for the specified event type(s), optionally passing parameters.
- The
.triggerHandler()
method is similar to the.trigger()
method, with the following exceptions:- Whilst the
.trigger()
method operates on all elements matched by the jQuery object, the.triggerHandler()
method only operates on the first matched element. - Events that are created using the
.triggerHandler()
method do not bubble up the DOM hierarchy and if not handled by the target element directly, do nothing. - The
.triggerHandler()
method does not cause the default behavior of an event to occur (such as a form submission). - The
.triggerHandler()
method returns whatever value was returned by the last handler it caused to be executed, so no chaining is possible. If no handlers are triggered, the.triggerHandler()
method returnsundefined
.
- Whilst the
Syntax
Signature | Description |
---|---|
.triggerHandler( eventType [, extraParameters] ) | Manually fire all event handlers on an element, for the specified event type, optionally passing parameters. |
.triggerHandler( event [, extraParameters] ) | Manually fire all event handlers on an element, for the specified event, optionally passing parameters. |
Parameters
Parameter | Description | Type |
---|---|---|
eventType | A string containing one or more DOM event types or custom event names.
| String |
extraParameters | Additional parameters to be passed to the event handler.
|
Array orPlainObject |
event | A jQuery.event object. | Event |
Return
Object returned from last handler or undefined
.triggerHandler( eventType [, extraParameters] )
ExampleTop
Manually fire all event handlers on an element, for the specified event type, optionally passing parameters.
In the example below when you enter or leave the 'div3' element the mouseenter
and mouseleave
JavaScript events fire.
When we press the left button we triggerHandler the mouseenter
JavaScript event manually. We are not passing across a parameter for any events so the output contains 'undefined' for the param
parameter.
When we press the right button we triggerHandler the mouseenter
JavaScript event manually. We are passing across the string '**PASSED PARAM**' for the param
parameter, so the output message
will contain this when fired from the triggerHandler or 'undefined' otherwise.
$(function(){
$('#div3').on({
'mouseenter.ns3': function(event, param) {
$(this).css({backgroundColor: 'teal', color: 'black'})
.append('**entering "div3". Passed param : ' + param);
},
'mouseleave.ns3': function() {
$(this).css({backgroundColor: 'maroon', color: 'white'})
.append('**leaving "div3" ');
}
});
$('#btn4').on('click', function(){
$('#div3').triggerHandler('mouseenter.ns3');
});
$('#btn5').on('click', function(){
$('#div3').triggerHandler('mouseenter.ns3', '**PASSED PARAM**');
});
});
div3. Some initial text.
.triggerHandler( event [, extraParameters] )
ExampleTop
Manually fire all event handlers on an element, for the specified event type, optionally passing parameters.
In the example below when you enter or leave the 'div3' element the mouseenter
and mouseleave
JavaScript events fire.
When we press the left button we triggerHandler the mouseenter
JavaScript event manually. We are not passing across a parameter for any events so the output contains 'undefined' for the param
parameter.
When we press the right button we triggerHandler the mouseenter
JavaScript event manually. We are passing across the string '**PASSED PARAM**' for the param
parameter, so the output message
will contain this when fired from the triggerHandler or 'undefined' otherwise.
$(function(){
$('#div8').on({
'mouseenter.ns8': function(event, param) {
$(this).css({backgroundColor: 'teal', color: 'black'})
.append('**entering "div8". Passed param : ' + param + '<br>');
},
'mouseleave.ns8': function() {
$(this).css({backgroundColor: 'maroon', color: 'white'})
.append('**leaving "div8" ' + '<br>');
}
});
$('#btn11').on('click', function(){
var ourEvent = $.Event('mouseenter.ns8');
$('#div8').triggerHandler(ourEvent);
});
$('#btn12').on('click', function(){
var ourEvent = $.Event('mouseenter.ns8');
ourEvent.param = "**PASSED PARAM**";
$('#div8').triggerHandler(ourEvent, ourEvent.param);
});
});
div8. Some initial text.
Related Tutorials
jQuery Advanced Tutorials - Lesson 4 - Event Handler Attachments