homepage Welcome to WebmasterWorld Guest from 54.243.23.129
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Select object value
Rain_Lover




msg:4463249
 3:33 am on Jun 9, 2012 (gmt 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

 

nameisbrian




msg:4463279
 6:32 am on Jun 9, 2012 (gmt 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])

Rain_Lover




msg:4463393
 5:49 pm on Jun 9, 2012 (gmt 0)

Brian,

Thanks for the confirmation and references!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved