I am trying to create a generic click event function for all the fields on a page which primarily checks for the target being blank or containing a user-entered value. However, I would like for this generic function to check for existence of a more specific function and, if one is found, to allow it to handle the validation of the target value.
example:
function genValueCheck(event) {
var af = event.target.id+'_checkValue';
if (typeof af == 'function') runFunction(af,[event]); // logic specific to this field
else {
// logic for checking for blanks, etc which applies to all fields
}
}
However, typeof returns ‘string’ in the above example, as it should since it is checking for the typeof ‘af’, and not what ‘af’ contains, i.e. the name of a function.
Is what I am attempting to do possible?
Thanks in advance.
That’s because
afis a string. If your function exists in the global scope, usewindow[af]. This should work for you:Demo