stopPropagation Test

by Richard Grable

HTML

<h1>Hello</h1>
    <div onkeypress="confirmParent(event)">
       <textarea onkeypress="confirmText(this, event)"></textarea>
    </div>
    <div id="text"></div>
    <p>
    <strong>NOTE:</strong>
    Notice how when pressing enter, the textarea now appends correctly, and we get the value desired. This is because <strong>event.stopPropagation()</strong> tells the chain of events to stop once its reached a certain child.
    </p>

JavaScript

var text = null;
var enterKey = 13;
document.addEventListener('DOMContentLoaded', function() {
	text = document.getElementById('text');
});

function confirmText(textbox, event) {
		var x = event.which || event.keyCode;
    if (x == enterKey) {
    	event.stopPropagation();
	    text.innerHTML = textbox.value;    
    }
    
}

function confirmParent(event) {
		var x = event.which || event.keyCode;
    if (x == enterKey) {
   		text.innerHTML="123";
    }
  }