Selecting a Specific Option

M

mike

I need to be able to select a specific option in a select list. This is
what I have:
<select name="mysel">
<option value=""></option>
<option value="1">First</option>
<option value="2" selected>First</option>
<option value="3">First</option>
</select>

But I want to select the first option. Here is my code:

var frm_objs = document.report.elements;
for ( j=0; j<frm_objs.length; j++ )
{
if ( frm_objs[j].type=='select-one' )
{
if ( frm_objs[j].name=='mysel' )
{
frm_objs[j].options[0].selected=true;
}
}
}

I think is should select the first ooption but it does not. The default
option 2 remains selected.

Any help is apreciated.
 
R

RobB

mike said:
I need to be able to select a specific option in a select list. This is
what I have:
<select name="mysel">
<option value=""></option>
<option value="1">First</option>
<option value="2" selected>First</option>
<option value="3">First</option>
</select>

But I want to select the first option. Here is my code:

var frm_objs = document.report.elements;
for ( j=0; j<frm_objs.length; j++ )
{
if ( frm_objs[j].type=='select-one' )
{
if ( frm_objs[j].name=='mysel' )
{
frm_objs[j].options[0].selected=true;
}
}
}

I think is should select the first ooption but it does not. The default
option 2 remains selected.

Any help is apreciated.

<html>
<head>
<script type="text/javascript">

window.onload = function()
{
var frm_objs = document.report.elements;
for ( j=0, len = frm_objs.length; j<len; j++ )
{
obj = frm_objs[j];
if ( obj.type=='select-one' //unnecessary
&& obj.name=='mysel' )
{
obj.options[0].selected=true;
}
}
}

</script>
</head>
<body>
<form name="report">
<select name="mysel">
<option value="0">Option 0</option>
<option value="1">Option 1</option>
<option value="2" selected>Option 2</option>
<option value="3">Option 3</option>
</select>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top