Created
March 25, 2012 21:59
-
-
Save boyofgreen/2200245 to your computer and use it in GitHub Desktop.
Hack #20: Using a JavaScript Delegate with Stored Custom Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<h1>Choose Your weapon</h1> | |
<p>Click on one of the selections below to find out more info about your character:</p> | |
<ul id="myList"> | |
<li data-discription="Most powerful goblin in entire kingdom" >Ludo</li> | |
<li data-discription="Ruler over all goblins big and small" >Jareth the Gobblin King</li> | |
<li data-discription="Only person who can put a stop to the Goblin King" >Sarah</li> | |
<li data-discription="Unsung hero of the goblin kingdom" >Hoggle</li> | |
</ul> | |
<p id="displayTarg" class="well"></p> | |
</div> <!-- /container --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mainElement = document.getElementById('myList'); | |
var discriptionTarget = document.getElementById('displayTarg'); | |
mainElement.addEventListener('click', function(e){ | |
var discription = e.target.getAttribute('data-discription'); //remember we use getAttribute instead of dataset.discription due to its backwards compatability | |
discriptionTarget.innerHTML = discription; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment