Can someone tel me the code to put a dropdown box showing default values as shown below using script ..i,e by enclosing them in document.write in script

<CODE>

<SELECT NAME="choice" >
<OPTION VALUE="1">one </OPTION >
<OPTION VALUE="2">two </OPTION >
<OPTION SELECTED VALUE=" 5 "> 5 </OPTION>
</SELECT>

</CODE>

this:

<script type="text/javascript">
document.write("<SELECT NAME=\"choice\" >"+
		"<OPTION VALUE=\"1\">one </OPTION >"+
		"<OPTION VALUE=\"2\">two </OPTION >"+
		"<OPTION SELECTED VALUE=\"5\"> 5 </OPTION>"+
		"</SELECT>");
</script>

thanz!That was the case of a constant..whats the case of variable? like now incase its a counter variable i need to show as default selected in a drop down box or the selected value changes dynamically according to some conditon....for example now i need to print a for loop counter variable's value as the selected option if a condition satisfies..

<script type="text/javascript">
document.write("<SELECT NAME=\"choice\" >")
for (var counter=0; counter<10;counter++)
{
if(counter==6)
{<!-- display as selected option-->
 }
else
{ <!-- display as an option in the dropdown box-->
}
}
document.write("</SELECT>");

</script>

thanz!That was the case of a constant..whats the case of variable? like now incase its a counter variable i need to show as default selected in a drop down box or the selected value changes dynamically according to some conditon....for example now i need to print a for loop counter variable's value as the selected option if a condition satisfies..

<script type="text/javascript">
document.write("<SELECT NAME=\"choice\" >")
for (var counter=0; counter<10;counter++)
{
if(counter==6)
{<!-- display as selected option-->
 }
else
{ <!-- display as an option in the dropdown box-->
}
}
document.write("</SELECT>");

</script>

then:

<script type="text/javascript">
document.write("<SELECT NAME=\"choice\" >")
for (var counter=0; counter<10;counter++)
{
if(counter==6)
{
document.write("<OPTION VALUE=\""+counter+"\" selected>"+counter+"</OPTION >");
 }
else
{ document.write("<OPTION VALUE=\""+counter+"\">"+counter+"</OPTION >");
}
}
document.write("</SELECT>");

</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.