Hello Readers,
bind() is the jquey method which is used to attaches one or more event handlers for selected elements and specifies a function to run when the event occurs.
syntax:
$(selector).bind(event,data,function)
event: event is a required field which specifies one or more events to attach to the elements.
data: data is optional which specifies additional data to pass along to the function.
function: function is a required field which specifies the function to run when the event occurs.
Example:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function handlerName(e){
alert(e.data.msg);
}
$(document).ready(function(){
$("p").bind("click", {msg: "You just clicked me"}, handlerName)
});
</script>
</head>
<body>
<p>Click me</p>
</body>
</html>
Pros and Cons of jquery bind() method
Pros
- This methods works across various browser implementations.
- It is pretty easy and quick to wire-up event handlers.
- The shorthand methods (.click(), .hover(), etc...) make it even easier to wire-up event handlers.
Cons
- The method attaches the same event handler to every matched element in the selection.
- It doesn't work for elements added dynamically that matches the same selector.
- There are performance concerns when dealing with a large selection.
0 Comment(s)