JavaScript - listbox - Asked By durgha subramanian on 26-Dec-05 02:51 AM

Hi,
I have a form, and in this form, there's a drop down menu, two buttons (labelled "add" and "remove") and finally, a listbox.
The question is, when the user clicks on the add button, how do I add the value selected in the drop down menu and put it into the listbox? After adding to the listbox, the selected value needs to stay in the drop down menu and not get removed. When the remove button is pressed, the selected value from the listbox just needs to disappear from the listbox and not the drop down menu.
Thanks in advance

adding & removing options from list box - Asked By mv ark on 26-Dec-05 04:15 AM

Try these functions -
function RemoveOptions() {
  field=document.forms[0].ListBox   //Your Listbox
  for( i=field.length-1; i>=0; i--) {	
    if(field.options[i].selected) {
      for(j=i;j<field.length-1;j++) {
        field.options[j].text=field.options[j+1].text
      }
    field.length--	
    }
  }
}
function AddOptions(text,value)
{
  var opt = new Option(text, value);
  var sel = document.forms[0].ListBox
  sel.options[sel.options.length] = opt;
}
with the buttons -
<INPUT TYPE="button" value="add" onclick="AddOptions(document.forms[0].SelectBox.options[document.forms[0].SelectBox.selectedIndex].text, document.forms[0].SelectBox.options[document.forms[0].SelectBox.selectedIndex].value)">
<INPUT TYPE="button" value="delete" onclick="RemoveOptions()">

Sample code from EE - Asked By Sathish S on 26-Dec-05 04:19 AM

<html>
<head>
<title>Add/Delete Options From A Select</title>
<script type="text/javascript">
function addOption(selectObject,optionText,optionValue) {
    var optionObject = new Option(optionText,optionValue)
    var optionRank = selectObject.options.length
    selectObject.options[optionRank]=optionObject
}
function deleteOption(selectObject,optionRank) {
    if (selectObject.options.length!=0) { selectObject.options[optionRank]=null }
}
function testAdd() {
    var formObject = document.testForm
    if (formObject.optionText.value!="" && formObject.optionValue.value!="") {
        addOption(formObject.fruitList,formObject.optionText.value,formObject.optionValue.value)
    } else {
        alert("Fill form and click Add")
    }
}
function testDelete() {
    var formObject = document.testForm
    if (formObject.fruitList.selectedIndex!=-1) {
        deleteOption(formObject.fruitList,formObject.fruitList.selectedIndex)
    } else {
        alert("Select an option and click Delete")
    }
}
</script>
</head>
<body>
<form name="testForm">
    <select name="fruitList" size="10">
        <option>Apple</option>
        <option>Kiwi</option>
        <option>Banana</option>
        <option>Peach</option>
        <option>Orange</option>
    </select>
    <br/>
    Fill form and click Add :<br/>
    Option Text : <input type="text" name="optionText"/>
    Option Value : <input type="text" name="optionValue"/>
    <input type="button" value="Add" onclick="testAdd()"/><br/>
    Select an option and click Delete : <input type="button" value="Delete" onclick="testDelete()"/>
</form>
</body>
</html>

I am getting un handled windows installer window - Asked By Pavan Reddy on 26-Dec-05 04:49 AM

In setup and deployment while installation and un installation i am getting a un handled window titled with windows installer
like 
window installer v 3.01.4000.1823
please help how to avoid this window
iterate through a multiselect listbox - Asked By durgha subramanian on 26-Dec-05 08:49 AM
hi,
  i have a multiselect listbox.this displays all the names of students.
on selection of multiple student names,their corresponding must b retrieved from the master table  'studdetail'(student name,studentid(primary key)).this student id retreived should be stored in another table .
how should i go through al the items selected and get the corresponding studentid.
the for loop which i used showed me an error.can u pls help me with the correct syntanx for 'for' loop to b used with asp.net.
foreach - Asked By F Cali on 27-Dec-05 01:23 AM
You can use a foreach to loop through the selected items in your multi-select listbox:
foreach (ListItem selectedItem in ListBox1.SelectedItems) {
string selectedValue = selectedItem.Value;
string selectedText = selectedItem.Text;
}