Reputation: 1
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?
Upvotes: 0
Views: 55
Reputation: 388316
There are 2 problems
First this.setFunction(fd);
should be this.setFields(fd);
Second
In your Mask
function, me
refers to the window
object, since it is an object me + ' ' + field
will return [object Window] #field1
, that is why you are getting th error.
So change $(me+' '+field)
to $(field)
Also as @plalx said, your use of local variables is useless as every instance of Mask
will override previous values
Upvotes: 3