bind
Syntax
.bind(eventType[, eventData], handler)
Parameters
eventType
- A string containing one or more JavaScript event types such as click, submit, or custom event names
eventData (optional)
- A map of data passed to the event handler
handler
- A function to execute
Return value
The jQuery object, for chaining purposes.
Description
Attach a handler to an event for the elements.
For example, to bind a mouseover event:
$("#objId").bind('mouseover', function(event){
// code here...
});
jQuery also has method shortcuts for creating event handlers.
$("#el").bind('click', function(event){
// do stuff
});
can be rewritten as:
$("#el).click(function(event){
// do stuff
});
bind click event
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("h1").bind("click", function(e){
var str = "( " + e.pageX + ", " + e.pageY + " )";
$("h1").text("Click happened! " + str);
});
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
bind the same action twice
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script>
$(function(){
$("#aDiv").bind('click', function(){
document.writeln("Handler 1");
});
$("#aDiv").bind('click', function(){
document.writeln("Handler 2");
});
});
</script>
</head>
<body>
<div id="aDiv" class="boxDiv">Press Me
</div>
</body>
</html>
Bind double click event to header
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("h1").bind("dblclick", function(e){
var str = "( " + e.pageX + ", " + e.pageY + " )";
$("h1").text("Click happened! " + str);
});
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
Mouse enter event
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("h1").bind("mouseenter mouseleave", function(e){
var str = "( " + e.pageX + ", " + e.pageY + " )";
$("h1").text("Click happened! " + str);
});
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
Mouse leave event
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("h1").bind("mouseenter mouseleave", function(e){
var str = "( " + e.pageX + ", " + e.pageY + " )";
$("h1").text("Click happened! " + str);
});
});
</script>
</head>
<body>
<body>
<div><h1>header 1</h1></div>
</body>
</html>
Home
JavaScript Book
jQuery
JavaScript Book
jQuery
Event:
- jQuery Event
- jQuery's methods for event
- Event object
- event.keycode
- event.clientX/clientY
- event.pageX/pageY: click event coordinates
- event.preventDefault()
- event.stopPropagation(): Stop only an event from bubbling by using the stopPropagation method.
- event.target.tagName
- event.which:check key code
- return false to Cancel a default action and prevent it from bubbling up
- bind
- .blur()
- .change()
- .click()
- .error()
- .dblclick()
- .delegate()
- die:Removes a bound live event
- .focus()
- .hover()
- keydown() event
- .keypress()
- keyup event and check the key code
- .live()
- .load()
- mousedown() event
- mouseenter() event
- mouseleave
- mousemove()
- mouseover() event
- mouseout
- mouseup() event
- .off() removes events
- .on() replaces the functionality of all the event methods.
- .one() method executes handler only once.
- .ready()
- .resize()
- .scroll()
- .select()
- .submit()
- .toggle()
- .trigger()
- .triggerHandler()
- .unbind() accepts a string describing the event type to unbind.
- .undelegate() removes the binding
- .unload()
- use bind/trigger to create custom event