Welcome to WebmasterWorld Guest from 54.147.189.54

Forum Moderators: open

Message Too Old, No Replies

Select object value

     
3:33 am on Jun 9, 2012 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Mar 1, 2010
posts: 194
votes: 0


Hi,

I just saw the following:
[w3schools.com ]

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

<!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
6:32 am on June 9, 2012 (gmt 0)

New User

joined:June 9, 2012
posts:4
votes: 0


Not only is your way more efficient, their way is flawed. Using their way, they assume that those are the only option elements that will be displayed on your page. Create another select form above theirs, and you'll see what I mean.

Although,
selectedIndex
is different than
value
. It depends on what you want to achieve with your code.

If you are using w3schools to learn JavaScript, you should stop, and use a better resource, such as MDN [developer.mozilla.org], or grab a decent book like: JavaScript, The Definitive Guide.

(I also recommend to look into unobtrusive JavaScript [en.wikipedia.org])
5:49 pm on June 9, 2012 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Mar 1, 2010
posts: 194
votes: 0


Brian,

Thanks for the confirmation and references!