Jump to content

Testing for a value with Java script


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...