I have an existing JavaScript function:
function changeColor(ID){
try{
initialize();
}
finally{
changeDesign(ID);
}
}
I want to do something like this:
for(var x=0; x<2; x++){
document.getElementById(x).onclick = "changeColor(" + x +")";
}
The output with the html should be:
<tr bgcolor="#FFFFFF" id="0" onclick="changeColor(0)>
ids should not begin with a digit.changeColor(x)calls the functionchangeColorand then the return value will be assigned to.onclick. If I dovar foo = bar(42);, thenbarwill be executed and the return value will be assigned tofoo. That's how function calling works (but now worries, people seem to get confused when it's about event handlers ;)).(0)because then you say: "Run the function and set the return value as onclick event".