Paulieboy Posted November 12, 2013 Share Posted November 12, 2013 New to Javascript. I am trying to show the selected value after the change so I can do some additional processing, but I cannot get my code to run. I am not sure my syntax is correct using java script.? <html> <head> <title>This is my test site</title> <script> function test(value) { value = document.getElementById("colorOption").selectedIndex; alert("document.getElementById("colorOption")[x].value); } </script> </head> <select id = "colorOption" name "colorOption" onchange = "test(value)"> <option value = 0>Red</option> <option value = 1>Green</option> <option value = 2>Blue</option> <option value = 3>Yellow</option> <option value = 4>Purple</option> </select> </body> </html> Link to comment Share on other sites More sharing options...
Ingolme Posted November 12, 2013 Share Posted November 12, 2013 You actually do have a syntax error here which is visible using a syntax highlighter. function test(value){value = document.getElementById("colorOption").selectedIndex;alert("document.getElementById("colorOption")[x].value);} The other problem is that you're not using the options property of the <select> element. function test() { var element = document.getElementById("colorOption"); var index = element.selectedIndex; var option = element.options[index]; alert(option.value);} 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