Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Items To A Combo Box

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
An HTML Form has a textbox & a combo box along with 2 buttons - ADD & REMOVE. When the Form is loaded, the combo box is empty. Now what I want is when a user enters some text in the textbox & clicks the ADD button, that text should get added to the combo box. Users should be allowed to enter as many no. of items as they like.Alternately when a user selects one item in the combo box & clicks the REMOVE button, that item should get deleted from the combo box. Again users should be allowed to delete as many no. of items as they like. How do I do this using JavaScript?

Thanks,

Arpan
 
Here's a script I wrote to manipulate combo boxes, you might find it useful in helping you do what you want.

<script>
option0 = new Option(&quot;Choice 0 Text&quot;,&quot;Choice0val&quot;)
option1 = new Option(&quot;Choice 1 Text&quot;,&quot;Choice1val&quot;)
option2 = new Option(&quot;Choice 2 Text&quot;,&quot;Choice2val&quot;)
option3 = new Option(&quot;Choice 3 Text&quot;,&quot;Choice3val&quot;)
option4 = new Option(&quot;Choice 4 Text&quot;,&quot;Choice4val&quot;)
option5 = new Option(&quot;Choice 5 Text&quot;,&quot;Choice5val&quot;)
optionArr = new Array(option0,option1,option2,option3,option4,option5)
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//this function loads the first select
function loadSelect(){
thisField = document.myForm.select1
for (x=1; x < optionArr.length; x++){ //Note &quot;x=1&quot; since you do not want to remove the first <option>
thisField.options[x] = null
}
for (x=0; x < optionArr.length; x++){
thisField.options[x] = optionArr[x];
//thisField.options[x].selected = true
}
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function moveVal(inBool){
theIndex = document.myForm.select1.selectedIndex
if (inBool){
if (theIndex > 0){
oldOptionVal = optionArr[theIndex].value
oldOptionTxt = optionArr[theIndex].text
oldOption = new Option(oldOptionTxt, oldOptionVal)
optionArr[theIndex] = optionArr[theIndex-1]
optionArr[theIndex-1] = oldOption
loadSelect()
document.myForm.select1.selectedIndex = theIndex - 1
}
}
else if (theIndex != -1){
if (theIndex < optionArr.length-1){
oldOptionVal = optionArr[theIndex+1].value
oldOptionTxt = optionArr[theIndex+1].text
oldOption = new Option(oldOptionTxt, oldOptionVal)
optionArr[theIndex+1] = optionArr[theIndex]
optionArr[theIndex] = oldOption
loadSelect()
document.myForm.select1.selectedIndex = theIndex + 1
}
}
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function resetVal(){
for (x=0; x < optionArr.length; x++){
optionArr[x] = eval(&quot;option&quot; + x)
}
loadSelect()
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function popSecond(){
//this function loads the second select using the primary select (select1)
if (document.myForm.second.value==&quot;Populate 2nd Select&quot;){
for (x=0; x<document.myForm.select1.length; x++){
newText = document.myForm.select1.options[x].text
newVal = document.myForm.select1.options[x].value
document.myForm.select2.options[x] = new Option(newText,newVal)
}
document.myForm.second.value = &quot;Erase 2nd Select&quot;
}
else{
for (x=document.myForm.select2.length-1; x>=0 ; x--){
document.myForm.select2.options[x] = null
}
document.myForm.second.value = &quot;Populate 2nd Select&quot;
}
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function popThird(){
//this function loads the third select using the option Array
if (document.myForm.third.value==&quot;Populate 3rd Select&quot;){
for (x=0; x<optionArr.length; x++){
newText = optionArr[x].text
newVal = optionArr[x].value
document.myForm.select3.options[x] = new Option(newText,newVal)
}
document.myForm.third.value = &quot;Erase 3rd Select&quot;
}
else{
for (x=document.myForm.select3.length-1; x>=0 ; x--){
document.myForm.select3.options[x] = null
}
document.myForm.third.value = &quot;Populate 3rd Select&quot;
}
}


</script>

<body onLoad=&quot;loadSelect()&quot;>

<form name=&quot;myForm&quot;>
<select name=&quot;select1&quot; multiple size=6><option></select>
<input type=button name=b1 value=&quot;Move Up&quot; onClick=&quot;moveVal(true)&quot;>
<input type=button name=b2 value=&quot;Move Down&quot; onClick=&quot;moveVal(false)&quot;>
<input type=button name=b3 value=&quot;Reset&quot; onClick=&quot;resetVal()&quot;>
<input type=button name=b3 value=&quot;Show&quot; onClick=&quot;viewArr()&quot;>
<br>
<select name=&quot;select2&quot; multiple size=6><option></select>
<input type=button onClick=&quot;popSecond()&quot; name=&quot;second&quot; value=&quot;Populate 2nd Select&quot;>
<br>
<select name=&quot;select3&quot; multiple size=6><option></select>
<input type=button onClick=&quot;popThird()&quot; name=&quot;third&quot; value=&quot;Populate 3rd Select&quot;>







Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top