I have a JavaScript object that is similar to this:
var MyClass;
MyClass = (function() {
// private
var $elems;
// constructor
function MyClass(selector) {
$elems = $(selector);
$elems.change(myEventHandler(e));
}
// event handler
function myEventHandler(e) {
var isSelected = ($(e.target).attr('checked') == 'checked');
if (isSelected) {
alert('You selected me');
}
}
return MyClass;
})();
I call this class in my html doc like this:
$(function(){
var myClass;
myClass = new MyClass(".MySelector");
});
Which gives me the error:
Uncaught ReferenceError: e is not defined MyClassJsFile.js:17
MyClass MyClassJsFile.js:17
(anonymous function) Step3:36
f.extend._Deferred.e.resolveWith jquery.min.js:16
e.extend.ready jquery.min.js:16
c.addEventListener.B
I thought that I was passing the event object to the event handler in this line of the constructor:
$elems.change(myEventHandler(e));
How do I pass the event correctly in this case?
You can use:
to just pass the reference to the right handler. As it is you’re invoking the handler immediately, and then attempting to bind the (non-existent) return value to the
changehandler.Your check for a selected box is over-complicated, BTW. You should be able to just write:
on the basis that jQuery will set
thisto be the actual DOM element that changed, and you can immediately check its booleancheckedproperty.