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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    populate an array from a select option list

    hi, i am trying to populate an array from a select box. Users select an item from the option list then they click the button below. The aim is to write to the text area what they have selected.

    <p align=center><INPUT TYPE="button" VALUE="Add selection to list" NAME="Adder" onclick="compilelist()";>

    function compilelist()
    {
    document.form1.text1.value=window.parent.returnlist();
    }

    . . . .(window.parent)

    var showlist= new Array();

    function returnlist()
    {
    var returnValue = "List so far\n";
    var listindex;
    var noofoptions=showlist.length;

    for (listindex=0; listindex<noofoptions; listindex++)
    {
    returnValue = returnValue + showlist[listindex] + "\n";
    }

    return returnValue;

    var findselected = window.frames["Rightside"].document.form1.oplist1.selectedIndex;
    var textofselected = window.frames["Rightside"].document.form1.oplist1.options[findselected].text;
    var valofselected = window.frames["Rightside"].document.form1.oplist1.options[findselected].value;

    showlist[showlist.length] = textofselected
    return true;
    }

    i have already been able to write to the text area but without making an array . . .like this:

    function compilelist()
    {
    var findselected = document.form1.oplist1.selectedIndex;
    var textofselected = document.form1.oplist1.options[findselected].text;
    var valofselected = document.form1.oplist1.options[findselected].value;

    var keeprevious=document.form1.text1.value;
    document.form1.text1.value=keeprevious + textofselected + " " + valofselected + "\n";
    }

    the problem with this technique is that i am limited for my options to be able to sort what i have written to the text box. i have to be able to add each selection as 1 array entry but i have no idea how. the code i have written to attempt this probly makes this clear.

    Can anyone suggest any way how this is possible please.? Thanks

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This any help?

    Code:
    <script>
    
    function make_array(what) {
      var tmp = new Array();
      for ( x=0; x<what.options.length; x++ ) {
        tmp[tmp.length] = what.options[x].innerHTML;
      }
      return tmp;
    }
    
    </script>
    
    <form>
    
    <select name="mydrop">
      <option>one</option>
      <option>true</option>
      <option>A</option>
      <option>yes</option>
      <option>first</option>
      <option>start</option>
    </select>
    
    <input type="button" value="Go" onclick="my_array=make_array(this.form.mydrop); alert(my_array);">
    
    </form>

  • #3
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks. your code has given me some ideas i'll try them and get back . . . however i think i may not have explained it too well so here is the problem in short -

    user selects an item from <select>

    user clicks button to add text value of <select> option to a text area
    . . each click adds option to the text area and to an array.

    the array needs to be defined by the choices the user makes.

  • #4
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi x_goose_x. i tried to change your code so the array would be that made from selectedIndex but i couldn't manage to do it and i havn't solved my problem. Any other ideas ?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script>
    
    my_arr = new Array();
    
    function do_stuff(what) {
      var val = what.my_drop.options[what.my_drop.selectedIndex].innerHTML;
      what.my_txt.innerHTML += val;
      my_arr[my_arr.length] = val;
    }
    
    </script>
    
    <form>
    
    <select name="my_drop">
      <option>one</option>
      <option>true</option>
      <option>A</option>
      <option>yes</option>
      <option>first</option>
      <option>start</option>
    </select>
    
    <textarea name="my_txt" style="width: 500px; height: 300px;"></textarea>
    
    <input type="button" value="Go" onclick="do_stuff(this.form);">
    
    </form>

  • #6
    New to the CF scene
    Join Date
    Apr 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    much appreciated x goose - thanks


  •  

    Posting Permissions

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