Home > other >  How do I stop the second onclick function from happening while the first function is executing?
How do I stop the second onclick function from happening while the first function is executing?

Time:10-05

I'm currently working on a validation feature. If the form fails to validate, it won't be able to submit, hence, I need to figure out a way to prevent the submitFilloutform() from happening.

<button 
  id="submit" 
  name="submit"
  type="submit"
  
  style="float: right;"
  onclick="validateForm(), submitFilloutform()"
>Submit</button>

Any help would be greatly appreciated. Thanks!

CodePudding user response:

Why not invoke the second function at the end block inside the first function?

<button 
  id="submit"
  name="submit"
  type="submit"
  
  style="float: right;"
  onclick="validateForm()"
>Submit</button>

function validateForm() {
  // xxx
  submitFilloutform()
}

function submitFilloutform() {
  // xxx
}

CodePudding user response:

onClick is executed line a function, so can include JS logic. Such as...

<button 
  id="submit" 
  name="submit" 
  type="submit"  
   
  style="float: right;" 
  onclick="if (validateForm()){ submitFilloutform();}"
>
Submit
</button>

This assumed that validateForm() returns a boolean value, of course.

  • Related