I'm working on this simple "Book List" task and I'm almost done with it, except that I would like to add a text under each book such as: text1, text2, text3, etc. I have an array that stores each text. As of right now I'm looping through my array but I keep getting "text12" for all the pictures. Basically I want to add a functionality that when the user clicks on a link for example "Murach's HTML5 and CSS3" I should get the picture as well as text1 and so on right below the picture. Can anyone tell me what I'm doing wrong please. Thank you so much in advanced.
CodePen: http://codepen.io/HenryGranados/pen/dMNpBV
Here's my javascript code:
var $ = function (id) {
return document.getElementById(id);
}
window.onload = function () {
// set up the list display
var categories = $("categories");
var h2Elements = categories.getElementsByTagName("h2");
var h2Node;
for (var i = 0; i < h2Elements.length; i++ ) {
h2Node = h2Elements[i];
// Attach event handler
h2Node.onclick = function () {
var h2 = this; // h2 is the current headingNode object
if (h2.getAttribute("class") == "plus") {
h2.setAttribute("class", "minus");
}
else {
h2.setAttribute("class", "plus");
}
if (h2.nextElementSibling.getAttribute("class") == "closed") {
h2.nextElementSibling.setAttribute("class", "open");
}
else {
h2.nextElementSibling.setAttribute("class", "closed");
var linkElements = h2.nextElementSibling.firstChild.childNodes;
}
$("image").setAttribute("src", "");
// needed for IE so a placeholder isn't displayed for the image
$("image").setAttribute("style", "display:none;");
}
}
// set up the image display
var listNode = $("categories");
var imageNode = $("image");
var texto = $("texto");
var imageLinks = listNode.getElementsByTagName("a");
var array = ["text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10", "text11", "text12"];
// Process image links
var i, linkNode, image;
for ( i = 0; i < imageLinks.length; i++ ) {
linkNode = imageLinks[i];
for(var j = 0 ; j < array.length; j++){
linkNodeArray = array[j];
// Attach event handler
linkNode.onclick = function (evt) {
var link = this; // link is the linkNode
var mitexto = this;
imageNode.src = link.getAttribute("href");
texto.innerHTML = linkNodeArray;
// needed for IE to display the image
imageNode.setAttribute("style", "display:block;");
// Cancel the default action of the event
if (!evt) { evt = window.event; }
if ( evt.preventDefault ) {
evt.preventDefault(); // DOM compliant code
}
else {
evt.returnValue = false;
}
}
// Preload image
image = new Image();
image.src = linkNode.getAttribute("href");
}
}
}