| From | Sent On | Attachments |
|---|---|---|
| Brice Burgess | Nov 29, 2006 1:57 pm | |
| Brice Burgess | Nov 29, 2006 2:14 pm | |
| Brice Burgess | Nov 29, 2006 3:01 pm | |
| Aaron Heimlich | Nov 29, 2006 3:28 pm | |
| Brice Burgess | Nov 29, 2006 3:33 pm | |
| Brice Burgess | Nov 29, 2006 3:39 pm | |
| Mike Alsup | Nov 29, 2006 4:23 pm | |
| Brice Burgess | Nov 29, 2006 4:50 pm | |
| Mike Alsup | Nov 29, 2006 5:02 pm | |
| Mike Alsup | Nov 29, 2006 5:10 pm | |
| Aaron Heimlich | Nov 29, 2006 5:13 pm | |
| Mike Alsup | Nov 29, 2006 6:30 pm | |
| Blair McKenzie | Nov 29, 2006 7:25 pm | |
| Aaron Heimlich | Nov 29, 2006 7:29 pm | |
| Blair McKenzie | Nov 29, 2006 8:59 pm | |
| Brandon Aaron | Nov 30, 2006 12:06 pm | |
| Jörn Zaefferer | Nov 30, 2006 12:08 pm |
| Subject: | [jQuery] Getting the $.val() of a select element in IE | |
|---|---|---|
| From: | Brice Burgess (b....@iceburg.net) | |
| Date: | Nov 29, 2006 2:14:54 pm | |
| List: | com.googlegroups.jquery-en | |
The $.val() function returns an empty value for a <select> element in IE if a value attribute is not specifically defined in each option. In Firefox the value (if not found as an attribute of an option) is extracted from the innerHTML/text of the element. Why this doesn't happen in IE is beyond me?
Here's the code;
---
<script type="text/javascript"> $().ready(function() { alert($('select').val()); $('select').change(function() { alert($(this).val()); }); }); </script>
<select> <option>a</option> <option SELECTED>b</option> <option>c</option> </select>
---
FF will alert "b" on load, and on changing the select, will alert "a","b", or "c" depending on what has been selected.
IE alerts "" on load, and continues to do so on change.
NOW --> If I change the <select> to read;
<select> <option value="a"/> <option SELECTED value="b"/> <option value="c"/> </select>
It works (alerts correctly), but the <select> does not display the values in the drop down menu -- so I must use
<select> <option value="a">a</option> <option SELECTED value="b">b</option> <option value="c">c</option> </select>
Which seems awfully redundant.. but is perhaps the W3C standard? Is this the only way to go? I still think it is strange that $.val() returns the value in FF but not in IE...
Thanks!
~ Brice





