473,324 Members | 2,535 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,324 software developers and data experts.

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

<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
5 17181
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ben | last post by:
Hello, I am currently learning PHP, and I have a problem with some variables. On the first page (choix.php), there is a form that lists product categories.This is the concerned part: echo...
4
by: mr_burns | last post by:
hi, using javascript, how do i select an option in a combo box? i have tried looking on the internet but dont really know what i should be searching for. what i want to happen is that when a...
2
by: Rani | last post by:
guys I've created 3 combo boxes for a month date and a year how do I set the value to be the default date ? is there a more suphisticated way to do so ? herein is the code i am using <select...
3
by: Dave | last post by:
I would like to have the word "Select" as the default option in a combo box that will retrieve its data from database. I also would like to have the ability to validate the combo box to ensure that...
1
by: sangram | last post by:
how to select a default value of combo box in run time, i have select some value from database ,depending on this i have to set the value of combox box... advance thanks..
12
by: Doogie | last post by:
How do I make a value the default value for a combo box in ASP 3.0? What I have below does NOT work, yet I've seen it in HTML file examples before (and works if I put it in a HTML file by itself,...
1
by: Doug White | last post by:
How do I make a value the default value for a combo box in ASP 3.0? What I have below does NOT work, yet I've seen it in HTML file examples before (and works if I put it in a HTML file by itself, but...
4
by: Doogie | last post by:
I posted this question a few days ago but I think that post is drying up and I still do not have an answer. I need to set a default option for a combo box that is not the first item in that combo...
6
by: robtyketto | last post by:
Greetings, Im a newbie to JSP/Javascript (unfortunalley). However I found enough example to create code that connects to my access db and populates two combo boxes. The selection of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.