Jump to content
Sign in to follow this  
Guest pixel13

getting/setting value property of a select object

Recommended Posts

Guest pixel13

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...