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?
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?
You need to make an anonymous currier:
num1.attachEvent("onkeypress", function() { return validateNum(num1, num2); });
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))
);
};
};
}