modern-web-design-and-development

(Brent) #1
1 $('#table td ').click(function() {

(^2) alert("The TD you clicked contains '"+$(this).text()+"'");
3 });
This simply says that whenever a

inside #table is clicked, alert its
text content.


bind()


We can do the same thing with bind, like so:


1 $('#table td ').click(function() {

(^2) alert("The TD you clicked contains '"+$(this).text()+"'");
3 });
Note that this time, the event type is passed as the first argument to bind
(), with the callback as the second argument. Why would you use bind()
over the simpler alias functions?
Very often you wouldn’t. But bind() gives you more control over what
happens in the event handler. It also allows you to bind more than one
event at a time, by space-separating them as the first argument, like so:
1 $('#table td ').bind('click', function() {
(^2) alert("The TD you clicked contains '"+$(this).text()+"'");
3 });
Now our event fires whether we’ve clicked the with the left or right
button. I also mentioned that bind() gives you more control over the
event handler. How does that work? It does it by passing three arguments
rather than two, with argument two being a data object containing
properties readable to the callback, like so:

Free download pdf