Guest pixel13 Posted October 26, 2010 Share Posted October 26, 2010 Hi all.In JS, I usually set/get the selected item of a select object using the selectedIndex property. Recently however I've discovered that the same operations can be done setting/getting the value property of the select object. I didn't even know that the select object had this property, but...try the following example:<select id="pippo"><option value="10" selected="selected">ten</option><option value="20">twenty</option><option value="30">thirty</option><option value="40">fourty</option><option value="50">fifthy</option></select><script type="text/javascript">document.getElementById('pippo').value=30;</script>You will see that the code works ("thirty" will be selected). I've tested it with several browsers (IE, FF, Safari). So, my question is: is it correct to use the "value" property of the select object? Is it supported by most browsers? And why in the W3School website there is no value property for the select object (see http://www.w3schools.com/jsref/dom_obj_select.asp) while some other sites shows it (see http://help.dottoro.com/ljcimvos.php)?Thanks in advance for any answer. Link to comment Share on other sites More sharing options...
aspnetguy Posted October 26, 2010 Share Posted October 26, 2010 I see this technique used a lot. I always set the selectedIndex but mostly just because that is what I have always done.This thread mentions that it might not be fully cross browser but the poster does not provide details when challenged. It appears to be cross browser safe. Just make sure you test in all the major browsers and you should be fine. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.