Home  >  Article  >  Web Front-end  >  How to Pass Parameters in jQuery\'s .click Event Handler?

How to Pass Parameters in jQuery\'s .click Event Handler?

Barbara Streisand
Barbara StreisandOriginal
2024-10-23 18:07:10290browse

How to Pass Parameters in jQuery's .click Event Handler?

jQuery's .click: Passing Parameters to User Functions

Implementing parameter passing in jQuery's .click event handler can be challenging. Although you can call a function without parameters using .click(add_event), passing parameters requires an alternative approach.

Solution 1:

jQuery version 1.4.3 introduced a new feature that allows you to pass a data map to the event object. This map is made available to the event handler function as its first parameter. To utilize this approach, follow these steps:

// Code Template
$("selector").click({param1: "Hello", param2: "World"}, cool_function);

// Function Implementation
function cool_function(event){
    alert(event.data.param1);
    alert(event.data.param2);
}

The above is the detailed content of How to Pass Parameters in jQuery\'s .click Event Handler?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn