The bind() is an inbuilt method in jQuery which is used to attach one or more event handlers for selected element and this method specifies a function to run when event occurs.
Syntax:
$(selector).bind(event, data, function);
Parameter: It accepts three parameters that are specified below-
- event: This is an event type which is passed to the selected elements.
- data: This is the data which can be shown over the selected elements.
- function: This is the function which is perform by the selected elements.
Return Value: It returns all the modification made on the selected element.
Code #1:
In the below code,”click” event are attached with the given paragraph using this method.
<html> <head> jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $("p").bind("click", function() { alert("Given paragraph was clicked."); }); }); </script> <style> p { display: block; padding: 50px; width: 280px; border: 2px solid green; } </style> </head> <body> <p>Click me!</p> </body> </html> |
Output:
Before clicking the “Click me” box-

After clicking the “Click me” box-

Code #2:
In the below code, same “click” event is added but this time data is also passed to this method.
<html> <head> jquery/3.3.1/jquery.min.js"></script> <script> function handlerName(e) { alert(e.data.msg); } <!--Here data is passing along with a function in bind method--> $(document).ready(function() { $("p").bind("click", { msg: "You just clicked the paragraph!" }, handlerName) }); </script> <style> p { display: block; padding: 50px; width: 280px; border: 2px solid green; } </style> </head> <body> <!--A pop will appear after clicking on this paragraph--> <p>Click me!</p> </body> </html> |
Output:
Before clicking the “Click me” box-

After clicking the “Click me” box-

Recommended Posts:
- Difference between on() and live() or bind() in jQuery
- How to bind 'touchstart' and 'click' events but not respond to both ?
- Underscore.js | _.bind() Function
- v-bind Directive in Vue.js
- Lodash _.bind() Method
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery | prepend() with Examples
- jQuery | dblclick() with Examples
- jQuery | offset() with Examples
- jQuery | children() with Examples
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- jQuery | focusin() with Examples
- jQuery | Animation, Slide methods with Examples
- jQuery | change() with Examples
- jQuery | filter() with Examples
- jQuery | height() and innerHeight() with Examples
- jQuery | eq() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


