View Full Version : onChange Event not happening ..(:

11-16-2005, 03:04 PM
I am creating a SELECT box dynamically using javascript.

dropBox = document.createElement("select");
dropBox.id = "drpBox";
dropBox.name = "drpBox";

I want to add ONCHANGE event to "dropBox".

I tried this, "dropBox.setAttribute ('onchange', 'someFunction(this)');"

It is not working ...(:

Please help me ...(:

11-16-2005, 04:56 PM
That looks OK but, I would have done this:

any valid code here


var someFunction=function(){


The setAttribute() method might work for this but, the syntax may be different than you expect. If I were to try using it in this regard, I would try:

dropBox.setAttribute ('onchange', 'alert("Hi")', 0)

But, that works in FF, not IE (just tested it). The setAttribute() method isn't always well supported for all attributes in IE.

11-16-2005, 07:12 PM
I tried this, "dropBox.setAttribute ('onchange', 'someFunction(this)');"Don't use the setAttribute method to add intrinsic event listeners. Use event properties, which expect a function reference:

dropBox.onchange = function() {

dropBox.setAttribute ('onchange', 'alert("Hi")', 0)The setAttribute method only has two arguments. Microsoft have added a third because of some odd case-sensitivity issues on their part, though that can be easily avoided.

The setAttribute() method isn't always well supported for all attributes in IE.Indeed. I would recommend avoiding the method when scripting HTML documents (including XHTML served as HTML).


11-16-2005, 11:25 PM
thanks mwinter & jscheuer1 , it worked...

But now, I got another issue... below code is inside another loop, which got 3 records and for each record i have to get all the Emp id and name. But say in this example,since we have 3 records, and below recordset contains only one record, rest of the array is showing me "UNDEFINED". Please help me...

//creates an array
var a1 = new Array();
a1[0]= new Array();
a1[1]= new Array();

<cfset counter = 0>

<cfloop query="query1">
a1[0][#counter#]= #empID#;
a1[1][#counter#]= #empname#;
<cfset counter = counter + 1>

11-18-2005, 06:17 PM
<cfset counter = 0>Sorry, never used ColdFusion.


06-17-2006, 12:31 AM
hey i found a solution for the same issue

objectname.setAttribute('onchange','someFunction()') didnt worked for me either.

but you can do it in on of these ways

method 1
objectname.onchange = function someFunction() {
//your code here

method 2
objectname.onchange = someFunction

//you define the function seprately here
function someFunction() {
//your code here

hope this will help

06-17-2006, 01:12 AM
method 2
objectname.onchange = someFunction

//you define the function seprately here
function someFunction() {
//your code here
}Actually, you'd have to define the function first.

06-17-2006, 03:54 AM
Twey is right and, these are basically the same solutions offered (correctly) by mwinter and myself earlier in this thread.

06-17-2006, 03:43 PM
objectname.onchange = function someFunction() {The identifier in the function expression is most likely redundant; an event listener is unlikely to want to call itself, especially without making it like a method call. It is especially important to note that the identifier, someFunction, should be propagated to any other scope outside the body of that function expression. That is to say that 'someFunction' should exist like a local variable within the function.

The snippet that I posted in my original post (http://www.dynamicdrive.com/forums/showpost.php?p=21156&postcount=3) is the most likely form that the assignment should take.

objectname.onchange = someFunctionUnless there is some other reason to reference the function, someFunction, a separate declaration is unnecessary and should be avoided.

Actually, you'd have to define the function first.If by that you mean:

function someFunction() {
/* ... */

objectname.onchange = someFunction;
you are incorrect. They are identical from a behavioural perspective.

When an execution context is entered (either 'global' code when the script is first run, or the body of a function when that function is called), all function declarations and variable declarations are immediately parsed before execution commences. All functions within scope will be available immediately.

Though variable declarations are handled straight away, initialisations are deferred until that statement is reached and executed. If the statement is in the block of some control statement, and the condition evaluates to false, the variable will still be declared, but it will not be initialised to the assigned value.