cha0sunity 0 Posted April 23, 2012 Report Share Posted April 23, 2012 I'm new to programming in general and am attempting to learn java script. I've been searching the internet trying to find the answer to this and I can not. I've learned how to use a text input with a submit button to collect and use data from a user. I've been trying to get a dropdown menu <select> </select> to take data from a user with the submit button. I know how to make the menu and populate it, but not with an array yet. Can anyone give me some advice on how to make this work? the tutorials on this site have helped but I can not get a good result. <html><head> <title>Another Test</title><script type="text/javascript"> function checkYear(theYear){if (theYear == 1985){alert ("it works");}else{alert ("broken");}} </script> </head> <body> <form method="POST" name="yearForm" onSubmit="checkYear(document.yearForm.theYear);"><select name="theYear" id="idYear" value="year"><option value="1985">1985</option><option value="1986">1986</option></select> <input type="submit" name="submit" value="submit" /> </form> </body</html> Quote Link to post Share on other sites
eTianbun 51 Posted April 23, 2012 Report Share Posted April 23, 2012 (edited) elem=document.getElementById('idYear'); selected=elem.options[elem.selectedIndex].value;alert('you selected: '+selected) Edited April 23, 2012 by eTianbun Quote Link to post Share on other sites
dsonesuk 913 Posted April 24, 2012 Report Share Posted April 24, 2012 You have told it what form object to look at! but not the important part the value selected function checkYear(theYear){if (theYear.value == 1985){alert ("it works");}else{alert ("broken");}} 1 Quote Link to post Share on other sites
eTianbun 51 Posted April 24, 2012 Report Share Posted April 24, 2012 You have told it what form object to look at! but not the important part the value selected function checkYear(theYear){if (theYear.value == 1985){alert ("it works");}else{alert ("broken");}} Does the <select> tag have value attr? Not aware of that! Quote Link to post Share on other sites
dsonesuk 913 Posted April 24, 2012 Report Share Posted April 24, 2012 The value is retrieved from the option selected, the value is sent through post/get from the name given to select element, whose value will be the option selected. 1 Quote Link to post Share on other sites
eTianbun 51 Posted April 24, 2012 Report Share Posted April 24, 2012 The value is retrieved from the option selected, the value is sent through post/get from the name given to select element, whose value will be the option selected. Thanks, i thought <select/> dont have such attr Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.