473,378 Members | 1,403 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,378 software developers and data experts.

assigning 'selected' to option in form select

Hi

<form nam="country" ...
<select name="industy" ...
<option ... selected... </option>
<option ...
Now, in Javascript, I want to change defaulted "selected" to fourth option.

document.country.industry.options[3].selected=true;

but I get an error. Is the syntax wrong?

Regards
John


--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 91027 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
Mar 23 '07 #1
4 17133
On Mar 23, 11:09 pm, "John" <john1...@yahoo.comwrote:
Hi

<form nam="country" ...
Dunno about "nam", it might be better to include a *name* attribute.
<select name="industy" ...
<option ... selected... </option>
<option ...

Now, in Javascript, I want to change defaulted "selected" to fourth option.

document.country.industry.options[3].selected=true;

but I get an error. Is the syntax wrong?
Yes, twice. Appart from the form's "nam" property, the select's name
is "industy", you are trying to access the form element collection's
"industry" property.

When posting code, copy and paste a "working" example. The easier you
make it for others, the better your chance of getting help.

<form name="country">
<select name="industry">
<option selected>1
<option>2
<option>3
<option>4
</select>
</form>

<script type="text/javascript">
document.country.industry.options[3].selected=true;
</script>

--
Rob

Mar 23 '07 #2

"RobG" <rg***@iinet.net.auwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
On Mar 23, 11:09 pm, "John" <john1...@yahoo.comwrote:
>Hi

<form nam="country" ...

Dunno about "nam", it might be better to include a *name* attribute.
><select name="industy" ...
<option ... selected... </option>
<option ...

Now, in Javascript, I want to change defaulted "selected" to fourth
option.

document.country.industry.options[3].selected=true;

but I get an error. Is the syntax wrong?

Yes, twice. Appart from the form's "nam" property, the select's name
is "industy", you are trying to access the form element collection's
"industry" property.

When posting code, copy and paste a "working" example. The easier you
make it for others, the better your chance of getting help.

<form name="country">
<select name="industry">
<option selected>1
<option>2
<option>3
<option>4
</select>
</form>

<script type="text/javascript">
document.country.industry.options[3].selected=true;
</script>

--
Rob
Hi

The nam and industy were typos.

Anyway, when I use

document.country.industry.options[3].selected=true;

I get an error.

When I use

var foo=document.getElementById("industry");
foo.options[3].selected=true;

it's OK.

There's a mystery.

Regards
John


--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 91248 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
Mar 23 '07 #3
John wrote:

>><form name="country">
<select name="industry">
<option selected>1
<option>2
<option>3
<option>4
</select>
</form>

<script type="text/javascript">
document.country.industry.options[3].selected=true;
</script>
>
Anyway, when I use

document.country.industry.options[3].selected=true;

I get an error.

When I use

var foo=document.getElementById("industry");
foo.options[3].selected=true;

it's OK.

There's a mystery.
It's a mystery to me, too. Using Rob G's example,the former is
correct, the latter, incorrect.
I'm guessing that you are checking this only in IE, am I right?

Mick
Mar 23 '07 #4
On Mar 24, 12:40 am, "John" <john1...@yahoo.comwrote:
"RobG" <r...@iinet.net.auwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
[...]
<form name="country">
<select name="industry">
<option selected>1
<option>2
<option>3
<option>4
</select>
</form>
<script type="text/javascript">
document.country.industry.options[3].selected=true;
</script>
[...]
>
Anyway, when I use

document.country.industry.options[3].selected=true;

I get an error.
In which browser? It works in IE, Safari and Firefox at least.
>
When I use

var foo=document.getElementById("industry");
foo.options[3].selected=true;
There is no element with an ID of "industry", so it shouldn't.
However, since IE thinks the name and ID attributes are the same
thing, it "works" in IE and perhaps in other browsers that copy IE's
idiosyncrasies.

The formal syntax is (wrapped for posting):

document.forms['country'].elements['industry']
.options[3].selected = true;

Which should work everywhere.

--
Rob

Mar 23 '07 #5

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

Similar topics

1
by: ouioui | last post by:
Hi, I try to have a communicatition between 2 windows In the first one i have a HTML styled select like that : <form name='addform' id='addform' action='add_dossier.php' method='post' >...
4
by: Mark Kolber | last post by:
I did a little searching on this but couldn't find an answer... On my website, I have a section of stories (www.midlifeflight.com/stories) There are different stores on different pages that are...
7
by: smash2004 | last post by:
i have a select field with multiple enabled so user can select multiple options... is it possible to get option that was clicked last...i need this because i need to check if this option was...
8
by: McKirahan | last post by:
Firefox does not reflect selected option via innerHTML How do I get Firefox to reflect selected option values? <html> <head> <title>FFinner.htm</title> <script type="text/javascript">...
2
by: Monty | last post by:
I use a SELECT dropdown as the nav interface for jumping to a chosen page number. When I setup up the SELECT element on the page, I want to show the user the current page number they are on, so, I...
6
by: Chameleon | last post by:
In the code below I want to alert the selected option of select. In Mozilla the code works (If I choose "3" alerts it). In IE alerts: nothing appear --------------------- <script> function hi()...
6
by: artev | last post by:
if I have a select with more options, how I can know if is there an option selected; is necessary a cycle? or is there an inner property?
7
by: fsalvador | last post by:
Hi, I am trying to figure it out how to to hide in onmouseout the select options of a form. Below is the code I got so far. It isn't working. Please help. Thanks <!DOCTYPE HTML PUBLIC...
1
by: vraamu | last post by:
here in this option transfer code.after transfering data from available _services multiple box to assigned_services multiple box i am unable to get the value of the assigned_services data for ex in...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.