Jump to content

Testing for a value with Java script


Paulieboy

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...