homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

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#: 4463247 posted 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#: 4463247 posted 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#: 4463247 posted 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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved