var div1 = document.querySelector("#div1");var div2 = document.querySelector("#div2");
div1.addEventListener("click", function (event) {alert("you clicked on div 1");}, true);
div2.addEventListener("click", function (event) {alert("you clicked on div 2");}, false);
var logElement = document.getElementById('log');
function log(msg) {if (logElement.innerHTML == "<p>No logs</p>")logElement.innerHTML = "";logElement.innerHTML += ('<p>' + msg + '</p>');}
function humanizeEvent(eventPhase){switch(eventPhase){case 1: //Event.CAPTURING_PHASEreturn "Event is being propagated through the target's ancestor objects";case 2: //Event.AT_TARGETreturn "The event has arrived at the event's target";case 3: //Event.BUBBLING_PHASEreturn "The event is propagating back up through the target's ancestors in reverse order";}}function capture(e) {log('capture: ' + this.firstChild.nodeValue.trim() + "; " +humanizeEvent(e.eventPhase));}
function bubble(e) {log('bubble: ' + this.firstChild.nodeValue.trim() + "; " +humanizeEvent(e.eventPhase));}
var divs = document.getElementsByTagName('div');for (var i = 0; i < divs.length; i++) {divs[i].addEventListener('click', capture, true);divs[i].addEventListener('click', bubble, false);}
p {line-height: 0;}
div {display:inline-block;padding: 5px;
background: #fff;border: 1px solid #aaa;cursor: pointer;}
div:hover {border: 1px solid #faa;background: #fdd;}