...

View Full Version : Function using only the last variable assigned.



ghostz00
06-23-2008, 06:31 PM
I can't seem to figure this out. I have two inputs, that I want to preform validation on after an onblur event. Problem is that once I loop through my function to set the onblur event's, The validation function fires onblur for both text inputs, but only checks the the last one.

So with this current code the validation only checks the date on both onblur events.



function setOnBlur(elm){
for (x in elm){
elm[x].onblur = function (){ validate(elm[x]); };
}
}

function validate(elm){

switch(elm.id){
case "date":
if(!elm.value.match("^(0[1-9]|[1-9]|1[012])[-/. ](0[1-9]|[1-9]|[12][1-9]|3[01])[-/. ](20[0-9]{2,2}|[0-9]{2,2})$")){
elm.className="error";
}
break;
case "workorder":
if (!elm.value.match("^[ ]*(w|W){0,1}[0-9]{5,5}[ ]*$")){
elm.className="error";
}
break;
}
}

setOnBlur([wo,date]); //the inputs are from document.getElementById;

liorean
06-23-2008, 06:56 PM
Every time through that loop, you're changing the value of the variable x. The function doesn't use the value of the variable at the time the function is created, it uses the value of the variable at the time the function is called, which will always be the last value given to it by the loop. Here's one way to change it to do what you want:
function setOnBlur(elm){
var x; // Make x a local instead of global variable.
function f(y){ // Save the value of x this iteration into y.
return function(){
return validate(elm[y]);
};
}
for(x in elm){
elm[x].onblur=f(x);
}
}

ghostz00
06-23-2008, 07:17 PM
thanks liorean, that worked perfectly and makes perfect sense now. I also changed from onblur to onchange event. I think that makes more sense.

Trinithis
06-23-2008, 07:31 PM
function setOnBlur(elm){
var x; // Make x a local instead of global variable.
function f(y){ // Save the value of x this iteration into y.
return function(){
return validate(elm[y]);
};
}
for(x in elm){
elm[x].onblur=f(x);
}
}

Doesn't IE have issues when using function declaration syntax when creating a local function at times? I've always gotten into the habit of creating local functions like so:


function setOnBlur(elm){
var x; // Make x a local instead of global variable.
var f = function(y){ // Save the value of x this iteration into y.
return function(){
return validate(elm[y]);
};
};
for(x in elm){
elm[x].onblur=f(x);
}
}

Are my concerns warrented?

liorean
06-23-2008, 08:04 PM
Doesn't IE have issues when using function declaration syntax when creating a local function at times? I've always gotten into the habit of creating local functions like so:

/ snip /

Are my concerns warrented?No, it has no problems with function declarations whatsoever. They work perfectly as advertised in JScript. JScript does have a problem with named function expressions though, which is easy to demonstrate:
var
f=function fn(){return fn},
fn=null;
alert(f()); // ==> nullAs you see, the identifier in a named function expression is bound to the containing scope in JScript and may, can and will be overwritten by variable assignments in the containing scope, while all other JavaScript engines correctly bind the identifier as a variable in the contained scope instead of the containing scope and thus are safe from outside interference of this kind.

Trinithis
06-23-2008, 08:25 PM
Now I remember what the issue was:
http://www.codingforums.com/showthread.php?t=129658&highlight=function



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum