There might be an easier way these days but this is the code I have used for years now:
it does the job :)
<script type="text/javascript">
function MoveOption(objSourceElement, objTargetElement){
if(objSourceElement.value==''){
alert('Please select an option to move.')
return false;
}
var aryTempSourceOptions = new Array();
var x = 0;
for (var i = 0; i < objSourceElement.length; i++){
if (objSourceElement.options[i].selected){
var intTargetLen = objTargetElement.length++;
objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
}else{
var objTempValues = new Object();
objTempValues.text = objSourceElement.options[i].text;
objTempValues.value = objSourceElement.options[i].value;
aryTempSourceOptions[x] = objTempValues;
x++;
}
}
objSourceElement.length = aryTempSourceOptions.length;
for (var i = 0; i < aryTempSourceOptions.length; i++){
objSourceElement.options[i].text = aryTempSourceOptions[i].text;
objSourceElement.options[i].value = aryTempSourceOptions[i].value;
objSourceElement.options[i].selected = false;
}
}
</script>
<select size="5" id="cat1" name="cat1" multiple>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
<option value="4">Value 4</option>
</select>
<input type="button" onClick="MoveOption(document.getElementById('cat1'), document.getElementById('cat2'));" value=">">
<input type="button" onClick="MoveOption(document.getElementById('cat2'), document.getElementById('cat1'));" value="<">
<select size="5" id="cat2" name="cat2" multiple>
</select>
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44:





by: udaydidigamPosted on 2009-08-11 at 00:41:35ID: 25066826
<html>
ionText,op tionValue) { lue) h nRank]=opt ionObject
optionRank ) { th!=0) { selectObject.options[optio nRank]=nul l }
ue!="" && formObject.optionValue.val ue!="") { List,formO bject.opti onText.val ue,formObj ect.option Value.valu e)
ctedIndex! =-1) { uitList,fo rmObject.f ruitList.s electedInd ex)
<head>
<title>Add/Delete Options From A Select</title>
<script type="text/javascript">
function addOption(selectObject,opt
var optionObject = new Option(optionText,optionVa
var optionRank = selectObject.options.lengt
selectObject.options[optio
}
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")
}
}
</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>