Solved

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

Posted on 2006-11-02
3
227 Views
Last Modified: 2008-02-07
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) {

           doSomeStuff(param1);
}

selectField.onchange=eventHandler("blah");



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

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


is this the correct way?





0
Comment
Question by:wrynn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
Basilisci earned 500 total points
ID: 17862501
This is ok:

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

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

selectField.onchange=eventHandler;

or

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
}
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question