...

View Full Version : populate an array from a select option list



acorn
04-05-2003, 12:27 PM
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

x_goose_x
04-05-2003, 03:53 PM
This any help?



<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>

acorn
04-06-2003, 01:09 AM
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.

acorn
04-07-2003, 03:14 PM
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 ?

x_goose_x
04-08-2003, 04:30 AM
<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>

acorn
04-09-2003, 12:31 AM
much appreciated x goose - thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum