anme98
anme98

Reputation: 94

How to return a value from a jQuery event function to the parent function?

I have a jQuery event inside a JavaScript function. I've already read that you cannot access the inner function. However, I would like to know how to adjust my code so that the parent function returns true or false depending on the jQuery function.

function validate() {
  $("#button").on('click', function(){
    var input = document.forms["formular"]["text"].value;
    if (input == "") {
      return false;
    }
  });
  if(onclickfunction() == true){
     return true;
  }
  else{
     return false
  }
}
validate();

Or can you recommend a different approach?

Upvotes: 0

Views: 72

Answers (1)

Not sure what this code is supposed to do, because calling validate only creates the event listener without actually executing it. But what you can do is to prevent the default action when you need, which is how validation is usually implemented:

$("#button").on('click', function(){
  var input = document.forms["formular"]["text"].value;
  yourSecondFunction(input !== "");
});

function yourSecondFunction(inputIsValid) {
    // Do your magic here
}

Upvotes: 2

Related Questions