View Full Version : selectbox

03-24-2009, 12:36 PM
Hi All,I am new to javascript.i want to know how can i generate selectbox dynamically using javascript.

what i mean is
for(int i=0;i<10;i++)
<select name="list" id="listid"><option>1</option></select>
every time i have to change the id of select tag.Any suggestions?

Thank You

Philip M
03-24-2009, 01:45 PM
This should point the way:-

<body onload = "load_combo ('sel_planet', planets)">

<select id= "sel_planet">

<script type = "text/javascript">

var planets = new Array ("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto");

function add_option (select_id, text) {
var select = document.getElementById(select_id);
select.options[select.options.length] = new Option(text);

function load_combo (select_id, option_array) {
for (var i = 0; i < option_array.length; i++) {
add_option (select_id, option_array[i]);


for(int i=0; i<10; i++) is incorrect syntax. Should be
for(var i=0; i<10; i++)

Tourist in Athens to tour guide: "You have told us something about Byzantine architecture. Would it be possible to meet some Byzants?"