Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <h1>List 1</h1>
  <ul class="list-1">
    <li>Click me</li>
    <li>Click me</li>
    <li>Click me</li>
  </ul>
  <h1>List 2</h1>
  <ul class="list-2">
    <li>Click me</li>
    <li>Click me</li>
    <li>Click me</li>
  </ul>
</body>
</html>
 
* {
  margin: 0;
  padding: 0;
}
body {
  padding: 14px;
  font: bold 40px/40px helvetica
}
h1 {
  font-size: 30px
}
ul {
  list-style-type: none;
  margin-bottom: 28px;
}
li {
  margin-bottom: 7px;
  padding: 14px;
  background: black;
  color: white;
  cursor: pointer;
  border: solid 1px black;
}
li:hover {
  background: white;
  color: black;
}
 
// Bind an event listener to each
// item in the list (bad)
$('.list-1 li').on('click', callback);
// Bind single 'delegate' listener
// to the list and trigger callback
// if event came from an decendent 
// matching the given selector (good)
$('.list-2').on('click', 'li', callback);
function callback() {
  alert('With delegate event handlers we get the same functionality with less overhead and more convenience :)');
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
wilsonpagepro
0viewers