YouTip LogoYouTip

Jq Event Type

## jQuery event.type Property The `event.type` property is a built-in feature in jQuery that returns the type of event that was triggered (for example, "click", "mouseover", or "submit"). This property is highly useful when you bind a single event handler function to multiple events and need to perform different actions based on which event was actually fired. --- ## Syntax and Usage To access the event type, pass the `event` object into your event handler function and access its `type` property: ```javascript event.type ``` ### Parameter Values | Parameter | Description | | :--- | :--- | | `event` | **Required.** The event object automatically passed from the jQuery event binding function. | --- ## Code Examples ### Example 1: Detecting Multiple Mouse Events In this example, we bind four different mouse events (`click`, `dblclick`, `mouseover`, and `mouseout`) to a `

` element. When any of these events are triggered, `event.type` is used to display the name of the active event inside a `

`. ```javascript $("p").on("click dblclick mouseover mouseout", function(event) { $("div").html("Event triggered: " + event.type + ""); }); ``` ### Example 2: Conditional Logic Based on Event Type You can use a `switch` statement or `if/else` blocks with `event.type` to run different code blocks depending on the user's interaction. ```javascript $("input").on("focus blur", function(event) { if (event.type === "focus") { // Highlight the input field when focused $(this).css("background-color", "#e0f7fa"); } else if (event.type === "blur") { // Clear the background color when focus is lost $(this).css("background-color", "#ffffff"); } }); ``` --- ## Key Considerations 1. **Standardization:** jQuery normalizes the `event.type` property across all browsers. You do not need to worry about legacy browser inconsistencies when retrieving the event name. 2. **Custom Events:** If you trigger a custom event using jQuery's `.trigger()`, `event.type` will return the name of your custom event: ```javascript $("p").on("myCustomEvent", function(event) { console.log(event.type); // Outputs: "myCustomEvent" }); $("p").trigger("myCustomEvent"); ``` 3. **Read-Only:** The `event.type` property is read-only. Attempting to manually overwrite this property during runtime will not change the native event behavior and is not recommended.
← Event WhichJq Event Timestamp β†’