Scope within JavaScript promise

I'm using parsley.js but having problems with the scope within the Javascript promise function.

form.parsley().whenValidate().done(function(){
    var form = $(this);
});

The above code returns undefined for var form.

The following does not work either:

var form = $('myform');

form.parsley().whenValidate().done(function(form){
    //do something with 'form'
});

1 answer

  • answered 2017-11-14 23:32 bart

    Solved it using bind()!

    form.parsley().whenValidate().done(function(){
        var form = $(this);
    }.bind($(this)));