The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Select object value
RainLover
post Jun 8 2012, 10:17 PM
Post #1


Advanced Member
****

Group: Members
Posts: 216
Joined: 16-November 09
Member No.: 10,346



Hi,

I just saw the following:
http://www.w3schools.com/jsref/prop_option_value.asp

And wonder if there's something wrong with selectObject.value. Why not a simple approach:

CODE
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function displayResult()
{
alert(document.getElementById("mySelect").value);
}
</script>
</head>
<body>

<form>
Select your favorite fruit:
<select id="mySelect">
  <option value="apple">Apple</option>
  <option value="orange">Orange</option>
  <option value="pineapple">Pineapple</option>
  <option value="banana">Banana</option>
</select>
</form>

<button type="button" onclick="displayResult()">Display value of selected fruit</button>

</body>
</html>


It seems to be working with no problem.

Thanks in advance!
Mike
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 9 2012, 12:12 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



Good question! Thanks for mentioning this, I've always been annoyed by the old verbose syntax.

According to http://quirksmode.org/js/forms.html#sselect "old browsers" don't support it, but it does seem to work in the ones I test with today. It also appears to be mentioned in HTML5:

QUOTE
select . value [ = value ]

Returns the value of the first selected item, if any, or the empty string if there is no selected item.

Can be set, to change the selection.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
RainLover
post Jun 9 2012, 12:42 PM
Post #3


Advanced Member
****

Group: Members
Posts: 216
Joined: 16-November 09
Member No.: 10,346



QUOTE(Christian J @ Jun 9 2012, 12:12 PM) *

According to http://quirksmode.org/js/forms.html#sselect "old browsers" don't support it


I even tried it in IE6 with no problem.

You're a star and I'm really grateful to you for your educational answers! smile.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th March 2024 - 03:02 AM