I am trying to create a list so that when the LI element is clicked, a specific div is displayed. I want to read the id of the div and other properties from a dictionary. So, the dictionary looks like this:
var ifSrc = {"welcome":"welcome.html",
"proceduresAndComputers":"../proceduresAndComputers/index.html",
"someId":"../someSourceFileForAnIFrame"}
and the function to form those LI elements
function createMenu(){
var listy = document.getElementById("sidebar");
for(var key in ifSrc){
var elem = document.createElement('li');
elem.innerHTML = key;
elem.className = "unselected";
elem.addEventListener("click",function(){openDiv(this,key);},false);
listy.appendChild(elem);
}
listy.firstElementChild.className="selected";
}
So, the Menu (UL with LI elements) looks ok, but no matter which LI I click on, the openDiv function passes the right value for this
, but same value for key
. In particular, it passes the last value for key
in the ifSrc
dictionary. However, the innerHTML of each elem (which is set to key
) appears fine. I checked the documentation for addEventListener and I couldn't find much.
Why is this happening? Any insights?