Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Intermediate
Mar 19, 1999

Dynamically Create Selection Box Options

You can create selection box options dynamically using JavaScript 3.0's Option object. The syntax for its constuctor is:
 
Option(text, value, [default selected], [selected])
Here is an example:
 
<HTML>
<BODY>

<form name=MyForm>
<script language = javascript>
function foo(){
    document.MyForm.select1.length=0;  //this clears the select box
    document.MyForm.select1.options[0] = new Option("<Select One>","0",true,true);
         for (var i=0; i < 20;i++)	{
	document.MyForm.select1.options[i+1] = new Option("Option" + i,"Value" + i);
	}
}			
</script>

<p><SELECT name="select1" style="HEIGHT: 22px; WIDTH: 340px">
</select>
<p><input type=button value="Populate" onclick="foo()">
</form>
</BODY>
</HTML>
William Wen
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date