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

assigning 'selected' to option in form select

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.