Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder ghostz00's Avatar
    Join Date
    Aug 2006
    Posts
    128
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Function using only the last variable assigned.

    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.
    Code:
    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;
    Greg

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    Code:
    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);
        }
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    ghostz00 (06-23-2008)

  • #3
    Regular Coder ghostz00's Avatar
    Join Date
    Aug 2006
    Posts
    128
    Thanks
    11
    Thanked 2 Times in 2 Posts
    thanks liorean, that worked perfectly and makes perfect sense now. I also changed from onblur to onchange event. I think that makes more sense.
    Greg

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Quote Originally Posted by liorean View Post
    Code:
    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:

    Code:
    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?
    Trinithis

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Trinithis View Post
    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:
    Code:
    var
        f=function fn(){return fn},
        fn=null;
    alert(f()); // ==> null
    As 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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    Trinithis (06-23-2008)

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Now I remember what the issue was:
    IE and prototype problem
    Trinithis


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •