Event Triggerhandler
## jQuery triggerHandler() Method
The `triggerHandler()` method triggers the specified event handlers attached to the selected elements.
While it is similar to the `.trigger()` method, `triggerHandler()` behaves differently in several key ways. Most notably, it does not trigger the default behavior of the browser (such as submitting a form or focusing an input field) and does not bubble up the DOM tree.
---
## Syntax
```javascript
$(selector).triggerHandler(event, [param1, param2, ...])
```
### Parameter Values
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `event` | *String / Event* | **Required.** Specifies the event type to trigger (e.g., `"click"`, `"submit"`, or a custom event). |
| `param1, param2, ...` | *Array / Object* | **Optional.** Additional parameters to pass along to the event handler. This is particularly useful for custom events. |
### Return Value
* Unlike most jQuery methods, `triggerHandler()` **does not support chaining** because it does not return a jQuery object.
* Instead, it returns the **return value** of the last handler executed.
* If no event handler is matched or triggered, it returns `undefined`.
---
## Key Differences: `trigger()` vs. `triggerHandler()`
Understanding the differences between these two methods is crucial for effective event handling in jQuery:
1. **Default Browser Behavior:**
* `.trigger()` triggers both the jQuery event handlers **and** the browser's default action (e.g., a `.trigger("submit")` will actually submit the form).
* `.triggerHandler()` **prevents** the browser's default action from occurring.
2. **DOM Bubbling:**
* Events triggered with `.trigger()` bubble up the DOM tree.
* Events triggered with `.triggerHandler()` **do not bubble** up the DOM hierarchy.
3. **Target Elements:**
* `.trigger()` affects **all matched elements** in the jQuery collection.
* `.triggerHandler()` only affects the **first matched element**.
4. **Method Chaining:**
* `.trigger()` returns the jQuery object, allowing you to chain further jQuery methods.
* `.triggerHandler()` returns the value returned by the event handler itself (or `undefined`).
---
## Code Examples
### Example 1: Basic Usage
Trigger the `select` event handler on an `` element when a button is clicked:
```javascript
$("button").click(function() {
$("input").triggerHandler("select");
});
```
### Example 2: Comparing `trigger()` and `triggerHandler()`
This example demonstrates how `.trigger()` triggers the browser's native focus behavior (causing the input to gain focus and show a cursor), while `.triggerHandler()` only executes the JavaScript function attached to the focus event.
```javascript
// Using .trigger() - This will run the handler AND focus the input field
$("#btn-trigger").click(function() {
$("input").trigger("focus");
});
// Using .triggerHandler() - This will ONLY run the handler, without focusing the input
$("#btn-triggerHandler").click(function() {
$("input").triggerHandler("focus");
});
// Bind a focus event handler to the input
$("input").focus(function() {
$("Focused!").appendTo("body").fadeOut(1000);
});
```
### Example 3: Passing Custom Parameters to Event Handlers
You can pass custom data to your event handlers using an array of parameters. This is highly useful for custom events:
```javascript
// Bind a custom event named "customGreet"
$("p").on("customGreet", function(event, name, profession) {
console.log("Hello " + name + ", the " + profession + "!");
});
// Trigger the custom event and pass parameters
$("p").triggerHandler("customGreet", ["Alice", "Developer"]);
// Output in console: "Hello Alice, the Developer!"
```
---
## Summary of Best Practices
* Use **`trigger()`** when you want to simulate a full, natural user interaction that includes browser defaults (like submitting a form or checking a checkbox) and you want the event to bubble up to parent containers.
* Use **`triggerHandler()`** when you only want to execute the JavaScript code bound to an event handler on a specific element, without affecting the browser state, causing page reloads, or bubbling up the DOM.
YouTip