if (document.order.cpu.options[document.order.cpu.selectedIndex].value
== "sempron3000") {var cpu = 85;}
the form is named order, and cpu is the select box name. The
"sempron3000" is the value I've given the option. Why is this working
in Firefox but not in Internet Explorer? Thank you in advance for your
help,
Chris
When posting code, post a minimal working example that demonstrates the
issue, or post a link. The following displays the described behaviour:
<script type="text/javascript">
function checkIt()
{
var x = document.order.cpu;
if (x.options[x.selectedIndex].value == "sempron3000") {
var cpu = 85;
}
alert(cpu);
}
</script>
<form name="order" action="">
<div>
<select name="cpu">
<option>sempron3000</option>
<option>blah</option>
</select>
<input type="button" value="Check value"
onclick="checkIt();">
</div>
</form>
>
> the form is named order, and cpu is the select box name. The
> "sempron3000" is the value I've given the option. Why is this working
> in Firefox but not in Internet Explorer? Thank you in advance for your
> help,
I suspect that you problem is that you haven't defined a value for the
option in the source HTML. The W3C HTML spec says that if no value
attribute is provided, the value of an option its content.
<URL:http://www.w3.org/TR/html4/interact/forms.html#adef-value-OPTION>
However, IE doesn't do that. If you don't give the option a value, you
have to explicitly get the option's text property.
There are two solutions: give the option value attribute a value or use
the text attribute.
Note that the code, as posted, has no syntax errors though it could be
much more concise. In your case, the value of the option could be '85'
and the text 'Sempron3000'. Now to get a value for local variable 'cpu'
you just get the value of the selected option, e.g.
<script type="text/javascript">
function checkIt()
{
var x = document.order.cpu;
var cpu = x.options[x.selectedIndex].value;
alert(cpu);
}
</script>
<form name="order" action="">
<div>
<select name="cpu">
<option value="85">sempron3000</option>
<option value="12">blah</option>
</select>
<input type="button" value="Check value"
onclick="checkIt();">
</div>
</form>
--
Rob
"Object doesn't support this property or method" w/o any visible reason
in IE may indicate a name conflict, because if IE's extra name space
for ID's.
Do you have by any chance a HTML element on your page with ID "order"
or "cpu"? Seems like the case.
Try explicit notation istead:
var sel = document.forms'[order'].elements['cpu'];
if (sel.options[sel.selectedIndex].value == "sempron3000")
{var cpu = 85;}
var sel = document.forms['order'].elements['cpu'];
[...]
--
Rob
Right! Thank you for correcting my typo.
P.S. Oh boy, here is comes now... :-)
Span elements don't have a name attribute, so that is invalid HTML.
> however I still received the error using the
> explicit notation.
Perhaps a consequence of IE's model of making NAMEs and IDs global
variables combined with how it supports invalid HTML.
[...]
--
Rob