473,480 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

options or selectedIndex property works in Firefox, not IE

I have a script that is working fine in the latest version of Firefox
with no errors or warnings, but does not work in IE. I have several
select boxes that, when changed, update some text (a price) on the
page. I'm checking what value is selected with an if statement,
setting a variable, then later updating the page. Here's the code that
gives me an "Object doesn't support this property or method" in IE but
works in Firefox:

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

Feb 14 '06 #1
7 7724
CF*******@gmail.com wrote:
I have a script that is working fine in the latest version of Firefox
with no errors or warnings, but does not work in IE. I have several
select boxes that, when changed, update some text (a price) on the
page. I'm checking what value is selected with an if statement,
setting a variable, then later updating the page. Here's the code that
gives me an "Object doesn't support this property or method" in IE but
works in Firefox:

if (document.order.cpu.options[document.order.cpu.selectedIndex].value
== "sempron3000") {var cpu = 85;}
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
Feb 14 '06 #2
VK
CF*******@gmail.com wrote:
I have a script that is working fine in the latest version of Firefox
with no errors or warnings, but does not work in IE. I have several
select boxes that, when changed, update some text (a price) on the
page. I'm checking what value is selected with an if statement,
setting a variable, then later updating the page. Here's the code that
gives me an "Object doesn't support this property or method" in IE but
works in Firefox:


"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;}

Feb 14 '06 #3
VK wrote:
[...]

Try explicit notation istead:

var sel = document.forms'[order'].elements['cpu'];

--------------------------^

var sel = document.forms['order'].elements['cpu'];

[...]
--
Rob
Feb 14 '06 #4
VK

RobG wrote:
VK wrote:
[...]

Try explicit notation istead:

var sel = document.forms'[order'].elements['cpu'];

--------------------------^

var sel = document.forms['order'].elements['cpu'];

[...]


Right! Thank you for correcting my typo.
P.S. Oh boy, here is comes now... :-)

Feb 14 '06 #5
I don't guess I made it clear in my first post but I did give all my
options values. The problem was that I did have span elements on the
page with the same name, however I still received the error using the
explicit notation. I tried changing the names of the spans and it
works perfectly now. Thank you very much for your help in pointing
that out.
Chris

Feb 14 '06 #6
CF*******@gmail.com wrote:
I don't guess I made it clear in my first post but I did give all my
options values. The problem was that I did have span elements on the
page with the same name
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
Feb 14 '06 #7
I just said "name", I used the id attribute. Sorry for the confusion.
I'm just glad it's fixed. Thank you,
Chris

Feb 15 '06 #8

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

Similar topics

4
9603
by: Paul Thompson | last post by:
I am getting an odd, inscrutable error in Mozilla Firefox. When I use an array to shift focus to an element, I get the error Error: " nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" ...
4
1859
by: Fluffy Convict | last post by:
I have found a workaround to disable certain options in an option list: var p = document.forms.elements; p.selectedIndex=-1; This works perfectly in IE - the selectedIndex becomes unclickable....
9
2108
by: Adam | last post by:
Hey, I'm trying to write a script with two standard drop down boxes. One contains days one contains the month. I want to update the options in the days box everytime the month is changed......
2
5190
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
3
1841
by: TKapler | last post by:
i got this simmple function, It stays on onChange on select and on change it hides all objects with ID equal to select name plus the optionvalue. It works perfectly in Opera and FF, but IE crashes...
3
3012
by: sewerized | last post by:
Hello all, I'm having a problem with a user preferences form I'm creating. I want to use dynamic dropdown SELECT boxes for this so my page doesn't get cluttered with 100000 links. Since I...
5
11938
by: jdzemke | last post by:
I am getting 'options' is null or not an object using when using a dynamic dataset with a dropdown list in an html form. I am filling-in a text field and filtering the values in the drop-down. ...
8
9654
by: grpramodkumar | last post by:
HI, function change(value,sub) { subcat = document.getElementById(sub); subcat.options.value = value; }
5
3283
by: Max | last post by:
Is there any way to set a select-multiple type <select multiple="multiple"with multiple selected options in scripting? Any idea about this is appreciative.
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.