473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Selecting a Specific Option

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.

Jul 23 '05 #1
2 2051
mike wrote:
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>

Jul 23 '05 #2
mike wrote:
I need to be able to select a specific option in a select list. This is
what I have:

<snip>

<head>
<script type="text/javascript">
function setselectedindex(n) {
var m=document.getElementById('mylist');
m.selectedIndex = n;
}
</script>
</head>

<body>
<select id='mylist'>
<option value=0>Zero</li>
<option value=1>One</li>
<option value=2>Two</li>
</select>
<br><br>
<input type='button' value='Set SelectedIndex to 0'
onclick="setselectedindex(0)";><br>
<input type='button' value='Set SelectedIndex to 1'
onclick="setselectedindex(1)";><br>
<input type='button' value='Set SelectedIndex to 2'
onclick="setselectedindex(2)";><br>
</body>
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
1
by: newcomer | last post by:
I have a javascript index that is similar to the one in the Windows help. It has a text field that allows the you to type text and it finds the closest item in the list below the text field. The...
1
by: jzhang29 | last post by:
I have a JSP page and it contains a dropdown list called Office. What I try to do is: When I select different office from this list, the information of office (address, phone,etc) will be...
4
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
1
by: =?ISO-8859-1?Q?Une_B=E9vue?= | last post by:
my xml fragment : <select id='aId'> <option>option 1</option> <option>option 2</option> <option>---?---</option> <option>option n + 1</option> <option>option n + 2</option>
4
by: kang jia | last post by:
hi i am doing mailinglist currently. the code in my first page is like this : : <html> <head> <link rel="stylesheet" type="text/css" href="gallery.css" /> <script language="JavaScript"> ...
2
by: .nLL | last post by:
got below script, works fine with IE but fails on opera. my js knowledge is very limited and can't find whats wrong with it. ---------------------------------------------- function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.