Last active
January 11, 2016 07:30
-
-
Save deenoize/9c5541ac8017eaed49be to your computer and use it in GitHub Desktop.
JS events listeners and show/hide function for pics
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
var simon = document.getElementById("simon"); | |
var bruce = document.getElementById("bruce"); | |
var ben = document.getElementById("ben"); | |
simon.addEventListener("click", picLink); | |
bruce.addEventListener("click", picLink); | |
ben.addEventListener("click", picLink); | |
function picLink() { | |
// Hide all pics | |
var allImages = ducument.guerySelectorAll("img"); | |
for (var i = 0; i < allImages.length; i++) { | |
allImages[i].className = "hide"; | |
} | |
// Then show this one. In html used an attribute data-img | |
var picId = this.attributes["data-img"].value; | |
var pic = document.getElementById(picId); | |
if (pic.className === "hide") { | |
pic.className = ""; | |
} else { | |
pic.className = "hide"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment