Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    what's wrong with the syntax?

    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]....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    627
    Thanks
    1
    Thanked 20 Times in 20 Posts
    You might try:

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

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    De gustibus non est disputandum.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    627
    Thanks
    1
    Thanked 20 Times in 20 Posts
    mordred's syntax is MUCH cleaner than mine; use his base to build this:

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

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •