Javascript: Append Textarea to Select Listbox

DMackallii
DMackallii used Ask the Experts™
on
I have a script (below) that ADDS the textarea contents into a Select List Box (dynamically) How do i modify it so it will APPEND the textarea contents to the Select List Box?

<html>
<body>
<form name="Mainform">
<textarea name="dataentry" rows="5" cols="45">

</textarea>
<input Type="Button" Name="loadbutton" Value="LOAD" onclick="BuildSelect()"><BR><BR>

<select name="dataadded" multiple>
</select>

</form><BR><BR>

<script Language="Javascript">
function BuildSelect(){
      var Options = document.forms[0].dataadded.options;
      Options.length = 0;
  var thearray = document.forms[0].dataentry.value.split(';');
  for (var i=0; i < thearray.length; i++) {
            Options[i] = new Option(thearray[i],thearray[i]);
  }
}
</script>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If I'm right the code will actually delete the existing entries and replace it by the new ones?

replace it by this one.

function BuildSelect(){
      var Options = document.forms[0].dataadded.options;
      var startIndex = Options.length;
  var thearray = document.forms[0].dataentry.value.split(';');
  for (var i=startIndex; i < thearray.length+startIndex; i++) {
            Options[i] = new Option(thearray[i],thearray[i]);
  }
}
Sorry... Scrambled with the index of the thearray variable.

Use this version:

function BuildSelect(){
      var Options = document.forms[0].dataadded.options;
      var startIndex = Options.length;
  var thearray = document.forms[0].dataentry.value.split(';');
  for (var i=0; i < thearray.length; i++) {
            Options[i+startIndex] = new Option(thearray[i],thearray[i]);
  }
}

Author

Commented:
That worked perfectlty. Thank You!

´*•.¸(`*•.¸?¸.•*´)¸.•*´
?«´•°*42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial