views:

254

answers:

2

I have to pass paramter the validateNum javascript function (eg. num1, num2)

if (num1.attachEvent) {
 num1.attachEvent("onkeypress", validateNum);  
}

How to pass. can i get code sample?

+1  A: 

You need to make an anonymous currier:

num1.attachEvent("onkeypress", function() { return validateNum(num1, num2); });  
SLaks
+1  A: 

Aside from SLaks' answer, in ECMAScript 5th Edition implementations you can use the bind method:

num1.attachEvent("onkeypress", validateNum.bind(null, num1, num2));

In implementations that don't support the method you can either use the Prototype JS framework or just add the method to the Function prototype with this snippet:

if (!('bind' in Function.prototype)) {
    Function.prototype.bind= function(owner) {
        var that= this;
        var args= Array.prototype.slice.call(arguments, 1);
        return function() {
            return that.apply(owner,
                args.length===0? arguments : arguments.length===0? args :
                args.concat(Array.prototype.slice.call(arguments, 0))
            );
        };
    };
}
Andy E
Do any real browsers support this?
SLaks
@SLaks: tbh I'm not sure, ECMAScript 5th was published in Dec 09. Even still, it doesn't hurt to add the method for forwards compatibility purposes.
Andy E
I'm not aware of any support in released browsers, but it's certainly expected to show up in forthcoming versions (eg. Firefox 3.7).
bobince