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 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2002
    Location
    Mumbai,India
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    populate combo from array

    currentTD = currentTR.insertCell();
    currentTD.innerHTML = "<div align='center'> <select name='conatinertype"+inputCount+"'> <option value='1'>OT</option> <option value='2'>TR</option> <option value='3'>GP</option> <option value='4'>RF</option> </select>";

    a row is inserted using javascript and above code generate combo .

    My

    The values are static
    How do I populate a combo from an array .

    I donot know the length of the same(or no. of entries not known_.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vikaspa
    currentTD = currentTR.insertCell();
    currentTD.innerHTML = "<div align='center'> <select name='conatinertype"+inputCount+"'> <option value='1'>OT</option> <option value='2'>TR</option> <option value='3'>GP</option> <option value='4'>RF</option> </select>";

    a row is inserted using javascript and above code generate combo .

    My

    The values are static
    How do I populate a combo from an array .

    I donot know the length of the same(or no. of entries not known_.
    never use innerHTML to generate combos, since they don't generate them properly.

    use the document.createElement and document.appendChild methods to create the selectbox, then loop thru the array and use the new Option constructor to add options to the dropdown.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,962
    Thanks
    0
    Thanked 236 Times in 233 Posts
    There are many ways to populate a combo, but do you mean it like this?
    Code:
    currentTD.innerHTML = '<div align="center"><select name="conatinertype'+inputCount+'">' + generateOptions() + '</select></div>';
    ...
    function generateOptions(){
      var opt='';
      for (var i=0;i<globalArray.length;i++){
         opt+='<option value="'+globalArray[i][0]+'">'+globalArray[i][1]+'</option>\n';
      }
      return opt;
    }
    This assumes that your array is a "2D" array, the first column refers to the option value, the 2nd column the option text.
    If the array is not global, you can pass it as the function parameter.

  • #4
    New Coder
    Join Date
    Oct 2002
    Location
    Mumbai,India
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks...thanks

    Dear glenngv


    Thanks a lot for your valued help

  • #5
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,

    Here's another method:
    Code:
    <form name='theForm'>
    <select name="theSel">
    </select>
    </form>
    <script type="text/javascript" language="javascript">
    var theArray = new Array('OT',1, 'TR',2, 'GP', 3, 'RF',4);
    
    var selObj = document.theForm.theSel
    for (var i = 0; i < theArray.length; i++)
      selObj.options[selObj.length] = new Option(theArray[i++], theArray[i])
    </script>
    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #6
    New Coder
    Join Date
    Oct 2002
    Location
    Mumbai,India
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Applause

    Dear Vinny

    I appreciate this

    your methos is also good one !

    Vikas

  • #7
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi Vikaspa,

    You're welcome

    Vinny
    Where the world once stood
    the blades of grass
    cut me still


  •  

    Posting Permissions

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