Solved

Can we add SELECT OPTION dynamically at client side

Posted on 2001-07-26
4
1,524 Views
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.

--javaq


<HTML>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
  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
  }
</SCRIPT>
<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()">
</FORM>
</BODY>
</HTML>
0
Comment
Question by:javaq092999
4 Comments
 
LVL 9

Accepted Solution

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


<!-- By Nimai -->
<html>
<head>
<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)
            return;
  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)
            return;
      var index = obj.selectedIndex;
      if(index != -1)
            obj.options[index]=null;
}

function removeAllItem(obj) {
      if(obj.length <= 0)
            return;
      var len = obj.length;
      for(i=0;i<len;i++)
            obj.options[0]=null;      
}

function transferItem(objFrom,objTo) {
      if(objFrom.length <= 0)
            return;
      var index = objFrom.selectedIndex;
      addItem(objTo,objFrom.options[index].value,objFrom.options[index].text);
      removeItem(objFrom,index);
}

function transferMultipleItems(objFrom,objTo) {
      if(objFrom.length <= 0)
            return;
      
      for(i=0;i<objFrom.length;i++) {
            if(objFrom.options[i].selected){
                  addItem(objTo,objFrom.options[i].value,objFrom.options[i].text);
                  removeItem(objFrom,i);
                  i--;
            }
      }
}

function transferAllItems(objFrom,objTo) {
      if(objFrom.length <= 0)
            return;
      var len = objFrom.length;
      for(i=0;i<len;i++) {
            addItem(objTo,objFrom.options[0].value,objFrom.options[0].text);
            objFrom.options[0]=null;
      }
}

function selectItem(obj,index) {
      if(obj.length <= 0)
            return;
      if(index != -1) {
            for(i=0;i<obj.length;i++)
                  obj.options[i].selected=false;
            obj.options[index].selected=true;
      }

}

function selectAllItems(obj) {
      if(obj.length <= 0)
            return;
      for(i=0;i<obj.length;i++)
            obj.options[i].selected=true;
}

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


//-->
</script>
<body>
<form>

<table><tr><td>
<SELECT SIZE="10" NAME="ro_lsttest" multiple>
  <OPTION SELECTED VALUE="1" >Test Item 1
  <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>
</td><td>
<SELECT SIZE="10" NAME="ro_lsttest1">
<OPTION SELECTED VALUE="1">Test Item 1
</SELECT>
</td></tr>
<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>
</table>
<br>
<table>
      <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>
</table>
</form>
LookUp List : http://www.siteexperts.com/tips/elements/ts22/page1.asp
Linking List Boxes to work together : http://www.siteexperts.com/tips/elements/ts20/page1.asp
</body>
</html>
0
 
LVL 1

Expert Comment

by:Y_ee
ID: 6329445
try this:
<HTML>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
 var count=0;
 function add(text,value) {
    var oOpt=new Option(text,value)
    var oSelect=document.MyForm.items
    oSelect.options[oSelect.options.length]=oOpt
 }

 function remove(index) {
    var oSelect=document.MyForm.items;
    oSelect.options[index]=null;
 }
</SCRIPT>
<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--)">
</FORM>
</BODY>
</HTML>

ciao
Y
0
 
LVL 1

Expert Comment

by:Moondancer
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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.
http://www.experts-exchange.com/questions/Q.20023290.html
http://www.experts-exchange.com/questions/Q.20121869.html
http://www.experts-exchange.com/questions/Q.20126064.html
http://www.experts-exchange.com/questions/Q.20139990.html
http://www.experts-exchange.com/questions/Q.20143257.html
http://www.experts-exchange.com/questions/Q.20148988.html
http://www.experts-exchange.com/questions/Q.20150711.html
http://www.experts-exchange.com/questions/Q.20158263.html
http://www.experts-exchange.com/questions/Q.20163902.html
http://www.experts-exchange.com/questions/Q.20163903.html
http://www.experts-exchange.com/questions/Q.20181528.html
http://www.experts-exchange.com/questions/Q.20245208.html
http://www.experts-exchange.com/questions/Q.20245590.html
http://www.experts-exchange.com/questions/Q.20251382.html
http://www.experts-exchange.com/questions/Q.20253111.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20143170.html
http://www.experts-exchange.com/questions/Q.20143993.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  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 http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

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.
 
Moondancer
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.
0
 

Author Comment

by:javaq092999
ID: 6881877
just closing!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), 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 …
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now