reading select element values in javascript in IE7
I ran into an issue with some Javascript code recently. I was using a jQuery statement to get a handle to an object for a select element input and I was then reading the selected value. The code looked something like this:
It worked great in Firefox, but it was not working in IE7 (not sure about other versions of IE as I only have IE7 available). In IE, instead of returning the value, I was getting an empty string back.
Turns out that in defining the select element itself, I was not explicitly defining the value for each option since this makes the page lighter and, according to the HTML standards, this is not a requirement. So my options looked like so:
As it turns out, the lack of a specified value was causing IE7 to gag... even though it worked fine in Firefox. So the options need to look like:
So the take-away message here, if you are defining select element, always define the value for each option even if it's not needed. It'll make your page a little bigger. But it may save you some trouble debugging your Javascript later.
var length = Number($('select[name="length"]')[0].value);
It worked great in Firefox, but it was not working in IE7 (not sure about other versions of IE as I only have IE7 available). In IE, instead of returning the value, I was getting an empty string back.
Turns out that in defining the select element itself, I was not explicitly defining the value for each option since this makes the page lighter and, according to the HTML standards, this is not a requirement. So my options looked like so:
...<option>1</option>...
As it turns out, the lack of a specified value was causing IE7 to gag... even though it worked fine in Firefox. So the options need to look like:
...<option value="1">1</option>...
So the take-away message here, if you are defining select element, always define the value for each option even if it's not needed. It'll make your page a little bigger. But it may save you some trouble debugging your Javascript later.
| Rating: | 100% positive, 2 total Votes |
| Categories: | javascript IE7 web programming jQuery |
| Added: | on Jul 17, 2009 at 1:28 pm |
| Added By: | an anonymous user |
| Searches: | ie7 javascript select jquery element |

