modern-web-design-and-development

(Brent) #1
1 $('#passengersTable td button').live('click', function() {

(^2) if (confirm("Are you sure you want to delete this
passenger?"))
(^3) $(this).closest('tr').remove();
4 });
Here, we apply the event with live() because the element to which it is
being bound (i.e. the button) did not exist at runtime; it was DOM-scripted
later in the code to add a passenger.
Handlers bound with live() are unbound with the die() method.
The convenience of live() comes at a price: one of its drawbacks is that
you cannot pass an object of multiple event handlers to it. Only one
handler.


5. .children() vs. .find()


Remember how the differences between parent(), parents() and
closest() really boiled down to a question of reach? So it is here.


children()


This returns the immediate children of an element or elements returned by
a selector. As with most jQuery DOM-traversal methods, it is optionally
filtered with a selector. So, if we wanted to turn all s orange in a table
that contained the word “dog”, we could use this:


1 $('#table tr').children('td:contains(dog)').css('background',
'#f90');
Free download pdf