onclick for button not calling function

后端 未结 3 1462
失恋的感觉
失恋的感觉 2021-01-25 13:33

One of the problems I am experiencing at the moment is not being able to call a function in the onclick event of the submit button.

3条回答
  •  抹茶落季
    2021-01-25 14:05

    You should write the validate() function outside the $(document).ready as the onclick in binded when the DOM loads - while $(document).ready scopes the function.

    This means the function is local to the closure and will not be visible globally if its written so inside $(document).ready.

    See demo below:

    function validate() {
      var contactName = document.getElementById("contact-name").value;
      alert("Thank you " + contactName);
    
    }

提交回复
热议问题