cha0sunity Posted April 23, 2012 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> Link to comment Share on other sites More sharing options...
eTianbun Posted April 23, 2012 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 Link to comment Share on other sites More sharing options...
dsonesuk Posted April 24, 2012 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 Link to comment Share on other sites More sharing options...
eTianbun Posted April 24, 2012 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! Link to comment Share on other sites More sharing options...
dsonesuk Posted April 24, 2012 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 Link to comment Share on other sites More sharing options...
eTianbun Posted April 24, 2012 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now