So I am trying to set up a function that will hide and show certain parts of the page, without the use of any outside libraries with Javascript. My problem seems to be that addEventListener is not attaching the event listener to the DOM=object but just running it.
The parts on the site I am using are:
<a class="tab" href="#index" id="index">Weapons</a>
<a class="tab" href="#armor" id="armor">Armor</a>
<a class="tab" href="#items" id="items">Items</a>
<div id="index_content" class="tab_content">
This is the content to display in weapons
</div>
<div id="armor_content" class="tab_content">
Welcome to armor!
</div>
<div id="items_content" class="tab_content">
Items are probably the best of the tabs.
</div>
My Javascript is:
function clear(key){
"use strict";
key = String(key);
//hides all content in items
for (var i = 0; i < itemArray.length; i++) {
document.getElementById(itemArray[i]+"_content").style.display = "none";
}
//shows current item
document.getElementById(key).style.display = "block";
return;
}
function tabsInit(){
"use strict";
for(var i = 0; i < itemArray.length; i++){
document.getElementById(itemArray[i]).addEventListener("click",clear(itemArray[i]));
}
}
window.onload = function(){
"use strict";
tabArray = document.getElementsByClassName("tab");
//add Items into item array
for(var i = 0; i < tabArray.length; i++){
itemArray[i] = tabArray[i].id;
}
tabsInit();
}