dlzone
asked on
JavaScript - adding/removing items from a ListBox
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
My bad, got it working
thanks for the help
greatly appreciated
thanks for the help
greatly appreciated
ASKER
Can I ask a follow-up question??
How do I check so that the items won't duplicate in the listbox?
How do I check so that the items won't duplicate in the listbox?
Hi All,
Try this, This may work for you.
<html>
<head>
<title>Add/Delete Options From A Select</title>
<script type="text/javascript">
function addOption(selectObject,opt ionText,op tionValue) {
if ( !recheck (selectObject, optionText, optionValue) ){
var optionObject = new Option(optionText,optionVa lue)
var optionRank = selectObject.options.lengt h
selectObject.options[optio nRank]=opt ionObject
document.testForm.optionTe xt.value = "";
document.testForm.optionVa lue.value = "";
}
else{
alert("Option is already present");
}
}
function deleteOption(selectObject, optionRank ) {
if (selectObject.options.leng th!=0) { selectObject.options[optio nRank]=nul l }
}
function testAdd() {
var formObject = document.testForm
if (formObject.optionText.val ue!="" && formObject.optionValue.val ue!="") {
addOption(formObject.fruit List,formO bject.opti onText.val ue,formObj ect.option Value.valu e)
} else {
alert("Fill form and click Add")
}
}
function testDelete() {
var formObject = document.testForm
if (formObject.fruitList.sele ctedIndex! =-1) {
deleteOption(formObject.fr uitList,fo rmObject.f ruitList.s electedInd ex)
} else {
alert("Select an option and click Delete")
}
}
function recheck (selectObject, optionText, optionValue){
var sltbool = false;
for (i=0; i<selectObject.options.len gth; i++){
if (optionText == selectObject.options[i].te xt || optionValue == selectObject.options[i].te xt ){
sltbool = true;
break
}
}
return sltbool;
}
</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>
Thanks
Muthu.
Try this, This may work for you.
<html>
<head>
<title>Add/Delete Options From A Select</title>
<script type="text/javascript">
function addOption(selectObject,opt
if ( !recheck (selectObject, optionText, optionValue) ){
var optionObject = new Option(optionText,optionVa
var optionRank = selectObject.options.lengt
selectObject.options[optio
document.testForm.optionTe
document.testForm.optionVa
}
else{
alert("Option is already present");
}
}
function deleteOption(selectObject,
if (selectObject.options.leng
}
function testAdd() {
var formObject = document.testForm
if (formObject.optionText.val
addOption(formObject.fruit
} else {
alert("Fill form and click Add")
}
}
function testDelete() {
var formObject = document.testForm
if (formObject.fruitList.sele
deleteOption(formObject.fr
} else {
alert("Select an option and click Delete")
}
}
function recheck (selectObject, optionText, optionValue){
var sltbool = false;
for (i=0; i<selectObject.options.len
if (optionText == selectObject.options[i].te
sltbool = true;
break
}
}
return sltbool;
}
</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>
Thanks
Muthu.
Hi Change this line to
if (optionText == selectObject.options[i].te xt || optionValue == selectObject.options[i].te xt ){
if (optionText == selectObject.options[i].te xt || optionValue == selectObject.options[i].va lue ){
if (optionText == selectObject.options[i].te
if (optionText == selectObject.options[i].te
May I ask, how are you creating this code? Using a tool?
ASKER
The user is supposed to choose from a pre-determined drop down list instead of free-typing into a text field.
Sorry, but I'm really new at this.