I'm doing a code for forms, as have a lot of forms need to simplify the way to mask fields in the forms.For I made a class that encapsulates the ids of the fields and assign specific interactions for each.
I have code:
var Mask=(function() {
var me=this;
var eventInterator;
var func;
var fields;
function Mask(fd,ev,fn){
this.setFunction(fd);
this.setFunction(fn);
this.setEvent(ev);
this.execute();
}
Mask.prototype.setFields = function(fd){
fields=fd;
}
Mask.prototype.setFunction= function(fn){
func=fn;
}
Mask.prototype.setEvent= function (ev){
eventInterator=ev;
}
Mask.prototype.execute = function(){
for (var i=0;i<fields.length;i++){
loadEvent(fields[i]);
}
}
function loadEvent(field){
$(me+' '+field).on(eventInterator,function() {
func();
});
}
return Mask;
})();
when I run the follow code:
function doSomeThing(){
alert("hi");
}
var fields = ['#field1','#field2','#field3','#field4'];
var mask = new Mask(fields,"keyup",doSomeThing);
I receive the error: Syntax error, unrecognized expression: [object Window]
how can I set the object of javascript class for the jquery selector?
this.setFunction(fd);tothis.setFields(fd);?Maskabstraction is useful vs$('#field1, #field2, #field3, #field4').keyup(doSomeThing);or$(fields.join(', ')).keyup(doSomeThing)if selectors have to be dynamic.