jQuery: delegate to first table row
March 11, 2011 09:03:21 Last update: March 11, 2011 09:03:21
Use the
:first selector to delegate an event to the first element of a selection.
- Code
- Demo
<!DOCTYPE html> <html> <head> <title>jQuery Delegate to First Child</title> <style type="text/css"> body { font-family: sans-serif; } table { border-collapse: collapse; } td { padding: 4px; border: #4488aa 1px solid; } </style> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script> <script type="text/javascript"> $(function() { $('table').delegate('tr:first', 'click', function() { alert('First row clicked'); }); }); </script> </head> <body> <h1>Delegate to First Child</h1> <table> <tr><td>First Row</td></tr> <tr><td>Second Row</td></tr> <tr><td>Third Row</td></tr> <tr><td>Fourth Row</td></tr> <tr><td>Fifth Row</td></tr> </table> </body> </html>