[resolved] Forms: Generate dropdown from textbox values
Posted: Wed Jan 07, 2009 6:17 am
Let's say we have one text box in a form, and a dropdown menu next to it. You can only enter numbers in the text box. If you enter "5" in the text box, the dropdown will have 5 alternatives: 5, 4, 3, 2, 1. And if you enter "2", the choices will be 2, 1.
Is there any way to do this?
Resolved:
Is there any way to do this?
Resolved:
Code: Select all
<script type="text/javascript">
function insertOptionBefore(num){
num = parseInt(num);
if (isNaN(num) || (num < 1) || num > 10) {
alert ("Invalid Entry!");
document.getElementById('txt1').value = "";
return false;
}
var cnt = num+1;
for (var i=1; i<=num+1; i++) {
cnt --;
var elSel = document.getElementById('selectX');
if (elSel.selectedIndex >= 0) {
var elOptNew = document.createElement('option');
elOptNew.text = 'Insert ' + cnt;
elOptNew.value = 'insert' + cnt;
var elOptOld = elSel.options[elSel.selectedIndex];
try {
elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
}
catch(ex) {
elSel.add(elOptNew, elSel.selectedIndex); // IE only
}
}
}
removeOptionSelected();
}
function removeOptionSelected() {
var elSel = document.getElementById('selectX');
for (var i = elSel.length - 1; i>=0; i--) {
if (elSel.options[i].selected) {
elSel.remove(i);
}
}
}
</script>
<form>
<select id="selectX" size="10" multiple="multiple">
<option value="0" selected="selected">Add Option Choices</option>
</select>
<br><br>
How many choices? <input type="text" id = "txt1" size = "2" value="" onblur="insertOptionBefore(this.value)" />
</form>