Question

I trying to invoke a public method of my jquery widget api from a form.submit but I'm having no success. Can anyone help me?

_cleanFormFields: function() {
  console.log("ok");
},

_addFormListener: function(map, marker) {
    var form = $(".add-form").clone().show();
    form.submit(function (event){
       event.preventDefault();
       _cleanFormFields();
    }
}           

Why this does not work?? The browser's console raises "Uncaught ReferenceError: _cleanFormFields is not defined" exception

Was it helpful?

Solution

_cleanFormFields is a property of some object, right? So you can't call it directly, you need to reference it via your object:

yourObject._cleanFormFields();

Or, depending on how _addFormListener() is invoked you might be able to use this. But you'd need to keep a reference of the this from _addFormListener() because within the .submit() callback this will be the form element in question:

_addFormListener: function(map, marker) {
    var form = $(".add-form").clone().show(),
        self = this;
    form.submit(function (event){
       event.preventDefault();
       self._cleanFormFields();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top