I need to have a drop-down menu that allows you to select a year from it (1900-2011). I am pretty sure that java-script has the functionality for that but I am totally lost on how to do it in code.
I am using PHP.
Wouldn't it be something along the lines of this?
while(i<2012){
add i to dropdown list
increment i
}
This is the current code I have for the form:
<form action="cite.php" method="post">
<h1>Newspaper</h1>
<script>
var select = document.getElementById("year");
for(var i = 2011; i >= 1900; --i) {
var option = document.createElement('option');
option.text = option.value = i;
select.add(option, 0);
}
</script>
<table width="100%">
<tr>
<td><h3>Author Name</h3></td>
<td><table width="100%" border="0">
<tr>
<td><label>
<input type="text" name="lastName" id="lastName" />
</label></td>
</tr>
<tr>
<td><label>
<input type="text" name="firstName" id="firstName" />
</label></td>
</tr>
</table></td>
</tr>
<tr>
<td><h3>Title of Article</h3></td>
<td><label>
<input type="text" name="artTit" id="artTit" />
</label></td>
</tr>
<tr>
<td><h3>Title of Newspaper</h3></td>
<td><label>
<input type="text" name="newsTit" id="newsTit" />
</label></td>
</tr>
<tr>
<td><h3>City</h3></td>
<td><label>
<input type="text" name="city" id="city" />
</label></td>
</tr>
<tr>
<td><h3>Date Published</h3></td>
<td><table width="100%" border="0">
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><select id="year" name="year"></select>
</td>
</tr>
</table></td>
</tr>
<tr>
<td><h3>Edition</h3></td>
<td><label>
<input type="text" name="edi" id="edi" />
</label></td>
</tr>
<tr>
<td><h3>Page Designation</h3></td>
<td><label>
<input type="text" name="page" id="page" />
</label></td>
</tr>
<tr>
<td><h3>Medium</h3></td>
<td><table width="200">
<tr>
<td><label>
<input type="radio" name="RadioGroup1" value="print" id="RadioGroup1_0" />
Print</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="RadioGroup1" value="web" id="RadioGroup1_1" />
Web</label></td>
</tr>
</table>
</td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="text" name="med" id="med" />
</label></td>
</tr>
<tr>
<td><input type="hidden" name="hiddenField" id="hiddenField" /></td>
<td><label>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</label></td>
</tr>
</table>
</form>
Assuming you have this element:
<select id="year" name="year"></select>
You can use this code:
var select = document.getElementById("year");
for(var i = 2011; i >= 1900; --i) {
var option = document.createElement('option');
option.text = option.value = i;
select.add(option, 0);
}
See it in action.
However: Why do you need to do this? Is it not possible to directly populate the drop down from the server side, which would be more natural?
Update: From PHP:
<td>
<select id="year" name="year">
<?php for($i = 2011; $i >= 1900; --$i)
printf('<option value="%d">%d</option>', $i, $i);
?>
</select>
</td>
var sel = document.getElementById('selectElementID');
var startAt = 1900;
var endAt = 2012;
for (i=0; i<=endAt; i++){
var opt = document.createElement('li');
opt.value = startAt + i;
opt.innerHTML = startAt + i;
sel.appendChild(opt)
}
JS Fiddle demo.
Or, if you'd prefer to use while
:
var sel = document.getElementById('selectElementID');
var startAt = 1900;
var endAt = 2012;
var i = 0;
while (i<=endAt){
var opt = document.createElement('option');
opt.value = startAt + i;
opt.innerHTML = startAt + i;
sel.appendChild(opt)
i++;
}
JS Fiddle demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With