Skip to content

Instantly share code, notes, and snippets.

@trivektor
Created November 11, 2011 19:26
Show Gist options
  • Select an option

  • Save trivektor/1358954 to your computer and use it in GitHub Desktop.

Select an option

Save trivektor/1358954 to your computer and use it in GitHub Desktop.
Exercise for JS Master Class (DOM Events)
<div id="the_div">
<ul id="the_list">
<li id="the_item">Click me!</li>
</ul>
</div>
<p id="log"></p>
<script type="text/javascript" charset="utf-8">
function log(string){
document.getElementById('log').innerHTML += (string + '<br/>');
}
document.getElementById('the_div').addEventListener(
'click', function(){ log('the_div!') }, true);
document.getElementById('the_list').addEventListener(
'click', function(){ log('the_list!') }, true);
document.getElementById('the_item').addEventListener(
'click', function(){ log('the_item!') }, true);
</script>
<!--
1. Change the addEventListener calls so that the events occur in the following order.
the_div! the_item! the_list! (change the 3rd param of the 2nd addEventListener to false)
2. Change the addEventListener calls so that the events occur in the following order.
the_item! the_list! the_div! (change the 3rd param in each addEventListener to false (useCapture = false which is useBubble=true)
3. Change the addEventListener calls so that the events occur in the following order.
the_item! (no other events) (change all 3rd params to false, and call event.stopPropagation() in the last addEventListener)
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment