Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use javascript to populate a html dropdown menu with values?

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>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</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>&nbsp;</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>
like image 955
willkara Avatar asked Dec 07 '11 16:12

willkara


2 Answers

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> 
like image 132
Jon Avatar answered Oct 04 '22 23:10

Jon


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.

like image 45
David Thomas Avatar answered Oct 05 '22 01:10

David Thomas