run js function with onchange event on selet option

robrodp
robrodp used Ask the Experts™
on
I have a form with a select

<select name="students" muliple>
<option value="student1">Student1</option>
<option value="student2">Student2</option>
<option value="student3">Student3</option>
<option value="student4">Student4</option>
</select>

I need to run a js function when options are clicked, something like:

<select name="students" muliple>
<option value="student1" onClick="check(this);">Student1</option>
<option value="student2" onClick="check(this);">Student2</option>
<option value="student3" onClick="check(this);">Student3</option>
<option value="student4" onClick="check(this);">Student4</option>
</select>

I have it like that but it is not running the function

the check(this) function is

<script>
      
      function test()
      {
alert("I am an alert box!");
      }
</script>

I would need the alrt box to appear when the options are checkd/unchecked. It is not happening
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2014
Top Expert 2014

Commented:
http://jsfiddle.net/yo31wdh8/

<select name="students" muliple onChange="test(this);">
    <option value="student1">Student1</option>
    <option value="student2">Student2</option>
    <option value="student3">Student3</option>
    <option value="student4">Student4</option>
</select>

Open in new window

<script>
function test(el) {
    alert("I am an alert box!");
    alert(el.value)
}
</script>

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
you function have the wrong name : http://jsfiddle.net/m9jobs85/1/
it's check instead test.

work too : http://jsfiddle.net/5xhsnwh7/
robrodpProgrammer

Author

Commented:
sorry the select does include the multiple option.
Expert of the Year 2014
Top Expert 2014
Commented:
You cannot capture a click on the OPTION tag in IE, so your original code (even fixed) will not work that way

You can get an array of the selected values.

If you really do need to know which one was clicked then you would be better looking at checkboxes or using a LIst tag
robrodpProgrammer

Author

Commented:
Thx for pointing that out

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial