Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-02
3
Medium Priority
?
228 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 2000 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

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.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

688 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