assigning 'selected' to option in form select

J

John

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
 
R

RobG

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>
 
J

John

RobG said:
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>

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
 
R

RobG

<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top