HTML select box question

Xenos

Member
Feb 12, 2000
72
0
0
How can I construct this drop down box with the 31 numbers in a month (1 to 31) without typing out each individual option thirtyone times? I have tried the following but it writes the numbers 'across' the dropdown window and not 'down'. Thanks.

<select name="ddSubscribe" size="1">
<option value="day">dd</option>
<option value="day">
<script type = "text/javascript">
for(var count=1; count<=31; count++) {
if(count<10) {
count = "0" + count;
}
document.write(count + '\n');
}
</script></option></select>
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
first, give the select an ID. then:



function addOption(selectbox, text, value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

var mySelect=document.getElementById("IdIgavemyselectBox");

for (var count=1;count<=31;count++)
{
addOption(mySelect, count, count);
}
 

Xenos

Member
Feb 12, 2000
72
0
0
Thanks PhatoseAlpha
but the thing still does not work. The id I gave the select is "ddSelect" and substituted it into your code. Was not sure about the quotes but I tried it with and without the quotes, still with no success. I am somewhat concerned about the "OPTION" in the first line of the function. What exactly is that? I may not be treating it correctly. Can you help further?

Another thing, I put the function in the head where I normally do. Any problems here?
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
The function should be able go just about anywhere, however the other potions need to go into the html somewhere below the element you're adding the options too.
If you must load it in the header, you'll need to turn the rest into it's own function, and call that in the onload for the document.
The issue is that you can't get an element by ID before it exists, and if the entire script is in the header, it will run before the element is created.


Quotes are neccessary.





The "OPTION" needs to be there exactly as is - it's the parameter to document.createElement, which tells the document what kind of element we are trying to create.

 

Xenos

Member
Feb 12, 2000
72
0
0
Many thanks PhatoseAlpha. The thing works like a dream. You're a scholar. Thanks once again.