• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

how do i assign an onchange function to a select field within my javascript code?

i want to dynamically assign an onchange function to a select field in my javascript code.

so lets say i have this code:

function eventHandler(param1) {



obviously this is the wrong way, what is the correct way?

selectField.onchange = function() { eventHandler("blah"); }

is this the correct way?

1 Solution
This is ok:

selectField.onchange = function() { doSomeStuff("blah"); };

If you don't need to specify any parameters, you can also do



selectField.onchange= doSomeStuff;

Another approach is to use addEventListener (allows to attach many listeners on the same event and element):

if (selectField.attachEvent) {
   selectField.attachEvent("onchange", function() { doSomeStuff("blah"); });  // this is for IE
} else {
   selectField.addEventListener("change", function() { doSomeStuff("blah"); }, false); // this for standards-compliant browser
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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