You canThe first thing you need to do thisis remove the parentheses from the function. What your code is currently expecting is for getCarModel
to return a couple of waysfunction which is then being called when the event is triggered.
The most simple, understandlbe wayIt looks like this is what you would like to use the that
variabledo:
var that;
$('#' + this.ID + ' .selMake').on('change', that.getCarModel);
Not this:
//$('#' This+ willthis.ID execute+ the' function.selMake').on('change', thisthat.getcarModelgetCarModel());
You can also useIf you want to call the bind
method in browsersfunction that support ES5.way, you can do as follows:
var that = this;
$('#' + this.ID + ' .selMake').on('change', thisfunction () {
that.getCarModel.bind(this);
});
Above, you are passing an anonymous function as an argument which will execute the code inside of it.
In the functions above, the definition of this
will depend on the element that triggered the event. If you're using underscore you might want the definition of this
to use itsbe tied to your _.bindthis
function in orderobject, you can do the following:
The most simple, understandable way is to make it cross-browser compatibleuse the that
variable:
var that;
$('#' + this.ID + ' .selMake').on('change', _that.bindgetCarModel); // This will execute the function this.getcarModel
You can also use the bind
method in browsers that support ES5.
$('#' + this.ID + ' .selMake').on('change', this.getCarModel. bind(this));