Last active
August 25, 2017 13:48
-
-
Save NoMan2000/be650467bd67f0d8fc072fdd4b71112a to your computer and use it in GitHub Desktop.
Some bear clicking!
This file contains 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
(function bearClickerStart(global, doc) { | |
// use strict is a configuration that prevents errors from javascript's older days | |
'use strict'; | |
// This is the root container that will hold everything else | |
let parent = doc.querySelector('#bear_insertion'); | |
parent.innerHTML = ''; | |
let bears = []; | |
// Create five bears | |
for (let i = 0; i < 5; i += 1 ) { | |
bears.push('http://placebear.com/g/200/300'); | |
} | |
// Loop over the five bears | |
for (let i = 0; i < bears.length; i++) { | |
let currentBear = bears[i]; | |
// create a parent div element which will have a span and an img as children. | |
let elem = doc.createElement('div'); | |
let img = doc.createElement('img'); | |
let p = doc.createElement('p'); | |
img.src = currentBear; | |
img.height = 200; | |
img.width = 300; | |
img.className = 'bear'; | |
img.dataset.clicks = 0; | |
p.textContent = "You have not clicked the bear!"; | |
elem.appendChild(img); | |
elem.appendChild(p); | |
parent.appendChild(elem); | |
} | |
// Create a function to handle the click event on the bear | |
let addBear = function (event) { | |
let target = event.currentTarget; | |
let clicks = Number(target.dataset.clicks) + 1; | |
target.nextSibling.textContent = "You clicked the bear " + clicks + " number of times!"; | |
target.dataset.clicks = clicks; | |
}; | |
// Select all bear classes | |
let bearClass = doc.querySelectorAll('.bear'); | |
// Loop over each bear and add a click listener to them. | |
Array.from(bearClass).forEach(function(el) { | |
el.addEventListener('click', addBear, false); | |
}); | |
}(window, document)); |
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Bear Clicker</title> | |
</head> | |
<body> | |
<div id="bear_insertion"> | |
</div> | |
<script src="bearclicker.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment