By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,694 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,694 IT Pros & Developers. It's quick & easy.

Beginner - How do I set a default option in a combo?

P: n/a
<select name="cboPlaces" id="cboPlaces">
<option value="3">Countryside</option>
<option value="4">Forest</option>
<option value="5">Mountain</option>
<option value="6">Desert</option>
<option value="7">Jungle</option>
<option value="7">Swamp</option>
<option value="8">River</option>
<option value="12">Town</option>
<option value="9">Sea</option>
</select>
Hello - when the combo loads, how do I set the default selected item
using javascript?

For instance I want the item to be the one with a value of 12, (Town)

How can I use the actual value of the item to make that item the
default displayed item?

Do I have to loop through every item in the combo and set the
selectedIndex to the item which has a value of 12? [I suspect I do
but I just want confirmation]

function doInit()
{
var combo = document.getElementById('cboPlaces');
for (var i=0 ; i <= combo.length; i++)
if (combo.value=='12')
combo.selectedIndex = i;
return true;
}

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Harry Haller wrote:
<select name="cboPlaces" id="cboPlaces">
<option value="3">Countryside</option>
<option value="4">Forest</option>
<option value="5">Mountain</option>
<option value="6">Desert</option>
<option value="7">Jungle</option>
<option value="7">Swamp</option>
<option value="8">River</option>
<option value="12">Town</option>
<option value="9">Sea</option>
</select>
Hello - when the combo loads, how do I set the default selected item
using javascript?

For instance I want the item to be the one with a value of 12, (Town)

How can I use the actual value of the item to make that item the
default displayed item?

Do I have to loop through every item in the combo and set the
selectedIndex to the item which has a value of 12? [I suspect I do
but I just want confirmation]

No, you need to loop through the menu values until you find what you're
looking for, but to service older browsers, you might try:

function doInit(){
var combo = document.forms[0].elements['cboPlaces'],cLen=combo.length;
for (var i=0 ; i < cLen; i++){
if (combo[combo.selectedIndex].value=='12'){
combo.selectedIndex = i;
return ;
}
}
}

Note in the for loop "i < cLen", and notice the return statement is
invoked as soon as you identify the index.
Mick
Jul 23 '05 #2

P: n/a
Harry Haller <Ha***@Steppenwolf.com> wrote in message news:i4********************************@4ax.com...
<select name="cboPlaces" id="cboPlaces">
<option value="3">Countryside</option>
<option value="4">Forest</option>
<option value="5">Mountain</option>
<option value="6">Desert</option>
<option value="7">Jungle</option>
<option value="7">Swamp</option>
<option value="8">River</option>
<option value="12">Town</option>
<option value="9">Sea</option>
</select>
Hello - when the combo loads, how do I set the default selected item
using javascript?

For instance I want the item to be the one with a value of 12, (Town)

How can I use the actual value of the item to make that item the
default displayed item?

Do I have to loop through every item in the combo and set the
selectedIndex to the item which has a value of 12? [I suspect I do
but I just want confirmation]

function doInit()
{
var combo = document.getElementById('cboPlaces');
for (var i=0 ; i <= combo.length; i++)
if (combo.value=='12')
combo.selectedIndex = i;
return true;
}


Avoid addressing form elements via document.getElementById.
The selections of a select box are addressed via its 'options' property.

This function will set a specified (non-multiple) <select>.
For simplicty, it does not validate the parameters or the 'disabled' attribute.

function setSelectByValue( formName, elemName, defVal )
{
var combo = document.forms[ formName ].elements[ elemName ], rv=false;

if( combo.type == 'select-one' )
{
for ( var i=0; i < combo.options.length && combo.options[i].value!=defVal; i++ )
;
if ( rv = (i != combo.options.length) )
combo.selectedIndex = i;
}

return rv; // true = success
}

setSelectByValue( 'myForm', 'cboPlaces', '12' );

--
Stephen Chalmers
547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134

Jul 23 '05 #3

P: n/a
Lee
Harry Haller said:

<select name="cboPlaces" id="cboPlaces">
<option value="3">Countryside</option>
<option value="4">Forest</option>
<option value="5">Mountain</option>
<option value="6">Desert</option>
<option value="7">Jungle</option>
<option value="7">Swamp</option>
<option value="8">River</option>
<option value="12">Town</option>
<option value="9">Sea</option>
</select>
Hello - when the combo loads, how do I set the default selected item
using javascript?


As far as I know, they're only called "combos" by Microsoft.
Why rely on Javascript?

<option value="12" selected>Town</option>

Jul 23 '05 #4

P: n/a
On 30 Jun 2005 09:08:08 -0700, Lee <RE**************@cox.net> wrote:
Harry Haller said:

<select name="cboPlaces" id="cboPlaces">
<option value="3">Countryside</option>
<option value="4">Forest</option>
<option value="5">Mountain</option>
<option value="6">Desert</option>
<option value="7">Jungle</option>
<option value="7">Swamp</option>
<option value="8">River</option>
<option value="12">Town</option>
<option value="9">Sea</option>
</select>
Hello - when the combo loads, how do I set the default selected item
using javascript?


As far as I know, they're only called "combos" by Microsoft.
Why rely on Javascript?

<option value="12" selected>Town</option>


There has to be a reason for using JavaScript. The form is being made
dynamically using ASP.NET. Of course, it's easier to use ASP.NET to
set the default. I still have other drop-down boxes that can be
created by loading values from a JavaScript array (instead of the
server-side database) so it's still something I need to know.

It's also something that a beginner needs to know and I just spent
ages looking for the info. using Google (groups and web search) and
finding nothing but honey traps for advertisers.

Jul 23 '05 #5

P: n/a
Lee
Harry Haller said:

On 30 Jun 2005 09:08:08 -0700, Lee <RE**************@cox.net> wrote:
Why rely on Javascript?


There has to be a reason for using JavaScript.


Yes, but my point is that, in general, you shouldn't do anything
with client-side scripting that you can do on the server side.
I'm not saying anything negative about Javascript.
You just can't assume that it's supported and enabled.

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.