Can we add SELECT OPTION dynamically at client side

Posted on 2001-07-26
Last Modified: 2010-05-18
I want to add <OPTION ....>Something</OPTION> *Dynamically* to a SELECT HTML control in a HTML page. How to do it? To illustrate what I am looking for, I am putting the code below.


  function add() {
     // How to add an OPTION to document.MyForm.options[] array at the end
     // something like ... options.add("1", "Hello");  // Value, Text

  function remove() {
     // How to remove an OPTION from document.MyForm.options[] for given index
     // something like ... options.remove("1");  // Value
<FORM NAME="MyForm">
      <SELECT NAME="items" SIZE=4></SELECT>
      <INPUT TYPE="BUTTON" NAME="Add" VALUE="Add" onClick="add()">
      <INPUT TYPE="BUTTON" NAME="Remove" VALUE="Remove" onClick="remove()">
Question by:javaq092999
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

nimaig earned 20 total points
ID: 6322141
Try this example :

<!-- By Nimai -->
<script language="javascript">
//This function is called by the buttons generated above and is used to move the selected item in the listbox up or down.
function move(f,bDir,sName) {
  var el = f.elements["ro_lst" + sName];
  if(el.length <= 0)
  var idx = el.selectedIndex
  if (idx==-1)
    alert("You must first select the item to reorder.")
  else {
    var nxidx = idx+( bDir? -1 : 1)
    if (nxidx<0) nxidx=el.length-1
    if (nxidx>=el.length) nxidx=0
    var oldVal = el[idx].value
    var oldText = el[idx].text
    el[idx].value = el[nxidx].value
    el[idx].text = el[nxidx].text
    el[nxidx].value = oldVal
    el[nxidx].text = oldText
    el.selectedIndex = nxidx

//Generic function that is used to sort the options array of the select box
function SelectTextSort(obj) { // sort by text
 var N=obj.options.length;
 for (var i=0;i<N-1;i++) {
   for (var j=i+1;j<N;j++) {
     if ( obj.options[i].text > obj.options[j].text ) {
       var i1= (obj.options[i].selected == true ) ? true : false
       var j1= (obj.options[j].selected == true ) ? true : false
       var q1 = obj.options[j].text;
       var q2 = obj.options[j].value;
       obj.options[j].text  = obj.options[i].text;
       obj.options[j].value = obj.options[i].value;
       obj.options[i].text  = q1;
       obj.options[i].value = q2;
       obj.options[i].selected = (j1 && true ) ? true : false
       obj.options[j].selected = (i1 && true ) ? true : false
 return true

function addItem(obj,value,text) {
      obj.options[obj.length]=new Option(text,value);

function removeItem(obj) {
      if(obj.length <= 0)
      var index = obj.selectedIndex;
      if(index != -1)

function removeAllItem(obj) {
      if(obj.length <= 0)
      var len = obj.length;

function transferItem(objFrom,objTo) {
      if(objFrom.length <= 0)
      var index = objFrom.selectedIndex;

function transferMultipleItems(objFrom,objTo) {
      if(objFrom.length <= 0)
      for(i=0;i<objFrom.length;i++) {

function transferAllItems(objFrom,objTo) {
      if(objFrom.length <= 0)
      var len = objFrom.length;
      for(i=0;i<len;i++) {

function selectItem(obj,index) {
      if(obj.length <= 0)
      if(index != -1) {


function selectAllItems(obj) {
      if(obj.length <= 0)

function getSelectedItem(obj) {
      if(obj.length <= 0)
      alert("value :" + obj.options[obj.selectedIndex].value);
      alert("text :" + obj.options[obj.selectedIndex].text);


<SELECT SIZE="10" NAME="ro_lsttest" multiple>
  <OPTION VALUE="2">TestItem 2
  <OPTION VALUE="3">Test Item 3
  <OPTION VALUE="4">Test Item 4
  <OPTION VALUE="5">Test Item 5
  <OPTION VALUE="6">Test Item 6
<SELECT SIZE="10" NAME="ro_lsttest1">
<tr><td><INPUT TYPE=button VALUE="Move Up" ONCLICK="move(this.form,true,'test')"><INPUT  TYPE=hidden  NAME="test"></td>
<td><INPUT TYPE=button VALUE="Move Down" ONCLICK="move(this.form,false,'test')"></td>
      <tr><td align="center"><INPUT TYPE=button VALUE="Sort List" ONCLICK="SelectTextSort(this.form.ro_lsttest)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Add Item" ONCLICK="addItem(this.form.ro_lsttest,'Value','Text')"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Remove Item" ONCLICK="removeItem(this.form.ro_lsttest)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Remove All" ONCLICK="removeAllItem(this.form.ro_lsttest)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Transfer Item" ONCLICK="transferItem(this.form.ro_lsttest,this.form.ro_lsttest1)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Transfer Multiple Items" ONCLICK="transferMultipleItems(this.form.ro_lsttest,this.form.ro_lsttest1)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Transfer All Item" ONCLICK="transferAllItems(this.form.ro_lsttest,this.form.ro_lsttest1)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Select Item" ONCLICK="selectItem(this.form.ro_lsttest,1)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Select All" ONCLICK="selectAllItems(this.form.ro_lsttest)"></td></tr>
      <tr><td align="center"><INPUT TYPE=button VALUE="Get Selected Item" ONCLICK="getSelectedItem(this.form.ro_lsttest)"></td></tr>
LookUp List :
Linking List Boxes to work together :

Expert Comment

ID: 6329445
try this:
 var count=0;
 function add(text,value) {
    var oOpt=new Option(text,value)
    var oSelect=document.MyForm.items

 function remove(index) {
    var oSelect=document.MyForm.items;
<FORM NAME="MyForm">
     <SELECT NAME="items" SIZE=4></SELECT>
     <INPUT TYPE="BUTTON" NAME="Add" VALUE="Add" onClick="add('hello',count++)">
     <INPUT TYPE="BUTTON" NAME="Remove" VALUE="Remove" onClick="remove(count--)">


Expert Comment

ID: 6877696
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
Thank you everyone.
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.

Author Comment

ID: 6881877
just closing!

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question