Agent Smith
Agent Smith

Reputation: 7

return boolean value higher up the chain in a function

I have the following function in jQuery:

function vali(id) {
    $(id).bind("jqv.field.result", function(event, field, errorFound, prompText) {
        $(id).removeClass('validationError');
        if(errorFound) {
          $(id).addClass('validationError');
          return false;
        };
        return true;
    });
}

I am adding a validation class to inputs in a complex form to style it appropriately when an input is invalid. I would also like to return a true or false value based on this. The 'validationError' class is being added correctly to inputs but I cannot return the boolean value as I suspect that I am calling a function within a function here and therefore need to return the value to the higher order function? Something like "return.return false;" Obviously this doesn't work but you get what I'm thinking on this. Maybe I am way off?

If I reference a specific input say for eg:

console.log( vali('#cause-name') ); 

the validationError styling works but I get 'undefined' in the console. Thanks

Upvotes: 0

Views: 161

Answers (1)

Ramesh
Ramesh

Reputation: 4293

This should work:

function vali(id) {
        var valid = true;
        $(id).bind("jqv.field.result", function(event, field, errorFound, prompText) {
            $(id).removeClass('validationError');
            if(errorFound) {
              $(id).addClass('validationError');
              valid = false;
            };
            valid = true;
        });
        return valid;
    } 

Upvotes: 1

Related Questions