I have a little problem with one of my javascript code. Here is the code
//assume array is an array containing strings and myDiv, some div in my doc
for(var i in array) {
var myString = array[i];
var a = document.createElement('a');
a.innerHTML = myString;
a.addEventListener("click", function() {myFunc(myString)}, false);
myDiv.appendChild(a)
}
function myFunc(s) {alert(s);}
However, since Strings are passed by reference in JavaScript, I see always the last string of my array when I click on the link a in question. Thus, my question is "How can I pass myString by value ?". Thank you for your help !
Phil
for...in.addEventListenerallows you to delegate an event, rather then binding it to each and every element individually, you're doing both and therefore neither: the worst of two worlds... +addEventListenerisn't supported by IE <9if (a.addEventListener){a.addEventlistener('click',make_callback(myString),false);} else { a.attachEvent('onclick',make_callback(myString));}