[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

run js function with onchange event on selet option

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
0
robrodp
Asked:
robrodp
  • 2
  • 2
1 Solution
 
GaryCommented:
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

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

work too : http://jsfiddle.net/5xhsnwh7/
0
 
robrodpAuthor Commented:
sorry the select does include the multiple option.
0
 
GaryCommented:
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
0
 
robrodpAuthor Commented:
Thx for pointing that out
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now