Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-05-2003, 11:27 AM   PM User | #1
acorn
New to the CF scene

 
Join Date: Apr 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
acorn is an unknown quantity at this point
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
acorn is offline   Reply With Quote
Old 04-05-2003, 02:53 PM   PM User | #2
x_goose_x
Regular Coder

 
Join Date: Jun 2002
Location: Montreal, Canada
Posts: 644
Thanks: 0
Thanked 0 Times in 0 Posts
x_goose_x is an unknown quantity at this point
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>
x_goose_x is offline   Reply With Quote
Old 04-06-2003, 12:09 AM   PM User | #3
acorn
New to the CF scene

 
Join Date: Apr 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
acorn is an unknown quantity at this point
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 is offline   Reply With Quote
Old 04-07-2003, 02:14 PM   PM User | #4
acorn
New to the CF scene

 
Join Date: Apr 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
acorn is an unknown quantity at this point
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 ?
acorn is offline   Reply With Quote
Old 04-08-2003, 03:30 AM   PM User | #5
x_goose_x
Regular Coder

 
Join Date: Jun 2002
Location: Montreal, Canada
Posts: 644
Thanks: 0
Thanked 0 Times in 0 Posts
x_goose_x is an unknown quantity at this point
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>
x_goose_x is offline   Reply With Quote
Old 04-08-2003, 11:31 PM   PM User | #6
acorn
New to the CF scene

 
Join Date: Apr 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
acorn is an unknown quantity at this point
much appreciated x goose - thanks
acorn is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 PM.


Advertisement
Log in to turn off these ads.