0

I have a function that validates a form and I want it to be triggered when the #submit-btn is clicked but the problem is that it is triggered every time the document is load.

$(document).ready(function(){
    $("#submit-btn").on('click', validar());
});
Juan Manuel Amoros
  • 347
  • 1
  • 5
  • 17

3 Answers3

1

Try this:

    $(document).on('click', '#submit-btn', function(){ 
      validar(); 
      return false; 
    })

There is no need for the $(document).ready(). But if the button is a submit button it never will triggered. Change the button type to "button" and after the validate return "true", submit your form manual

$('#formId').submit()
TPeter
  • 11
  • 1
1

You are passing the result of your function validar() as the EventHandler - effectively calling it the moment the document is loaded.

Just remove the brackets and you'll have the function mapped as EventHandler

Adwaenyth
  • 2,020
  • 12
  • 24
0

Try this

$(document).ready(function(){

    //console.log("Document ready event occurred")

    $(document).on('click','#submit-btn', function(e){

       //console.log("Submit button clicked")

       e.preventDefault();
       validar()

    });
});
Shyju
  • 214,206
  • 104
  • 411
  • 497