...

View Full Version : what's wrong with the syntax?



Kor
07-16-2003, 03:46 PM
function bla(){
var s = new Array(12)
for(i=0;i<=11;i++){
s[i] = "s"+i;
}
var sel = new Array(12)
for(i=0;i<=11;i++){
sel[i] = document.forms[0].s[i].selectedIndex;
}
alert(s[7]);
alert(sel[7]);
}

i can see s[7] value, but not the sel[7]...

i have 12 <select> in page, named "s0", "s1", "s2"....."s11". I want to use the selectedIndex values for further operation... But I want to make it in short way, using arrays... It seems I can not substitute s1, s2, ... names with s[i]....

jalarie
07-16-2003, 03:54 PM
You might try:

&nbsp;Xeq='sel['+i+']=document.forms[0].sel'+i+'.selectedIndex;';
&nbsp;eval(Xeq);

mordred
07-16-2003, 03:56 PM
You have to use the forms.elements collection to make a dynamic lookup of form elements by their name:

document.forms[0].elements['s' + i].selectedIndex;

jalarie
07-16-2003, 04:00 PM
mordred's syntax is MUCH cleaner than mine; use his base to build this:

&nbsp;sel[i]=document.forms[0].elements['s' + i].selectedIndex;

Kor
07-16-2003, 04:06 PM
thank you all, it works now. It came completely out of my mind that the full syntax is, indeed:

document.forms[name].elements[name].propriety



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum