How to distinguish between javascript submit and manually by clicking submit

how to distinguish javascript triggered submit and manually click submit form
code example below

function myfunction() { document.getElementById("id_searchform").submit(); return true; } 

the form:

 <div class='row'> <div class='col-md-4'> <div class='clszipcode' ><span>Enter Zipcode</span></div> </div> <div class='col-md-4'> <div class='clstxtzipcode' ><input type="text" name="zip_code" id="txtZipcode"></div> </div> <div class='col-md-4'> <div class='clsbtnzip' ><input type="submit" name="submit" id="btnSearch" value="Search" class="button_example" ></div> </div> </div> <a href="#" onclick="return myfunction();" >click to submit</a> 
+6
source share
1 answer

Let me see if I understand:

  • You want to determine if the user clicked the link to submit the form.
  • You want to determine if the user clicked the submit button to submit the form.
  • You have another function called validate() that will somehow use this information.

If so, consider using a variable to save if the link was clicked before running the form to submit.

  • Initialize the global variable wasClicked to false
  • When clicking a link, set wasClicked to true
  • The trigger wasClicked form after wasClicked set.
  • Run validate() when submitting the form
  • Check if(wasClicked){...} in validate()

Here is a working example

+1
source

Source: https://habr.com/ru/post/987491/


All Articles