jQuery delegate to all but the first table row 

Joined:
02/21/2009
Posts:
158

March 11, 2011 09:28:31    Last update: March 11, 2011 09:28:31
To delegate an event to all but the first child, the selector :not(:first) does not work, but :gt(0) works.

  • Code
  • Demo
<!DOCTYPE html>
<html>
<head>
	<title>jQuery Delegate to all but the 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() {
	    // tr:not(:first) selector does not work!
		$('table').delegate('tr:not(:first)', 'click', function() {
			alert('I\'m not the first row!');
		});

		// tr:gt(0) works
		$('table')
		.delegate('tr:gt(0)', 'dblclick', function() {
			alert('Double clicked, not the first row');
		});
	});
	</script>

</head>

<body>
	<h1>Delegate to all but the first row</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>
Share |
| Comment  | Tags