[resolved] Forms: Generate dropdown from textbox values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sk8bft
Forum Newbie
Posts: 14
Joined: Sat Jan 03, 2009 5:55 pm

[resolved] Forms: Generate dropdown from textbox values

Post by sk8bft »

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:

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>
Last edited by sk8bft on Wed Jan 14, 2009 8:55 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Forms: Generate dropdown from textbox values

Post by VladSun »

Yes - use JavaScript and HTML DOM.
There are 10 types of people in this world, those who understand binary and those who don't
sk8bft
Forum Newbie
Posts: 14
Joined: Sat Jan 03, 2009 5:55 pm

Re: Forms: Generate dropdown from textbox values

Post by sk8bft »

Thank you! I'll search around and read a bit to see if I can make it. If someone knows about a simple tutorial about this, I'd appreciate if you could post a link.
sk8bft
Forum Newbie
Posts: 14
Joined: Sat Jan 03, 2009 5:55 pm

Re: Forms: Generate dropdown from textbox values

Post by sk8bft »

I'm stuck. The opposite thing is easier (assigning values from a dropdown to the textbox):

Code: Select all

 
<head>
<script type="text/javascript">
function favBrowser()
{
var mylist=document.getElementById("myList");
document.getElementById("favorite").value=mylist.options[mylist.selectedIndex].text;
}
</script>
</head>
 
<body>
<form>
Select your favorite browser:
<select id="myList" onchange="favBrowser()">
  <option>Internet Explorer</option>
  <option>Netscape</option>
  <option>Opera</option>
</select>
<p>Your favorite browser is: <input type="text" id="favorite" size="20"></p>
</form>
</body>
 
</html>
 
Can anyone help me with this?
Post Reply