The Masta
The Masta

Reputation: 857

Problems with option selection in Chrome and Opera

I've Problems with thise Code, it's working fine in Firefox and Internet Explorer but it doesn't work with Opera and Chrome Browsers...

<script>
function planetselect()
{
optionen=document.getElementById('pstart').options;
for(i=0;i<optionen.length;i++)
{
if(optionen[i].value==67080)
{
  optionen[i].setAttribute('selected','selected');
  }
}
optionen=document.getElementById('pdest').options;
for(i=0;i<optionen.length;i++)
{
if(optionen[i].value==67080)
{
  optionen[i].setAttribute('selected','selected');
  }
}
}</script>

Upvotes: 0

Views: 599

Answers (2)

JGibbers
JGibbers

Reputation: 257

Did you make sure you close the <script> tag? I can't really see a problem with your code that you posted, so either you didn't close your tag, or your optionen or options variables aren't there or valid

Also too, you should know that chrome has a javascript console that should show you any errors you have. To open it, it's ctrl-shift-j. That should help you a lot.

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382170

Change

optionen[i].setAttribute('selected','selected');

to

optionen[i].selected = true;

More generally, avoid the use of setAttribute to change DOM properties. Sometimes it works, sometimes it doesn't.

From the MDN :

Using setAttribute() to modify certain attributes, most notably value in XUL, works inconsistently, as the attribute specifies the default value. To access or modify the current values, you should use the properties. For example, use elt.value instead of elt.setAttribute('value', val).

Upvotes: 1

Related Questions