0

I cannot get my button to trigger the javascript. This seems to be quite straightforward? What is keeping the alert from showing up?

 <script type="text/JavaScript" 
 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">

  $(document).ready(function ()
  {
    $("#submitButton").click(function (e) {

            alert("Hello!!");
               
      });
   });
</script>


<input type="submit" name="submitButton" id="submitButton" class="form-input" value="ButtonName" 
OnClick="ActionName" runat="server" />
John
  • 371
  • 2
  • 4
  • 16
  • 1
    A submit button submits the form, which causes the page to (at the least) reload. You would need `e.preventDefault()` in order for the `alert` to show. – Heretic Monkey Apr 05 '21 at 16:59
  • Does this answer your question? [Form submit button onclick does not call my JS function](https://stackoverflow.com/questions/35221978/form-submit-button-onclick-does-not-call-my-js-function) – Heretic Monkey Apr 05 '21 at 17:03

1 Answers1

1

You should end of the script tag if you add source only. And you should add extra script tag for internal script. And input type should be button because you are not submitting a form in here. If you will submit something, you should use a form element and listen it's submit event. You can use input type="submit" for it.

$(document).ready(function ()
  {
    $("#submitButton").click(function (e) {

            alert("Hello!!");
               
      });
   });
 <script type="text/JavaScript" 
 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<input type="submit" name="submitButton" id="submitButton" class="form-input" value="ButtonName" 
OnClick="ActionName" runat="server" />
umutcakir
  • 773
  • 6
  • 8
  • But I do need to post the form back to the server. It doesn't do that anymore. The entire form is within a BeginForm and has an onlick value. Can I still maintain both the front and back end? – John Apr 05 '21 at 17:12
  • 1
    You can still use submit type. I edited code. – umutcakir Apr 05 '21 at 17:37