Nathan
Nathan

Reputation: 43

Adding on click events to buttons in MVC Razor

I've been trying to call a javascript function from a button click, and I can't figure out what I'm doing wrong.

I've searched for similar questions and followed the answers, but the button click is still not calling the function.

The following code is in my .cshtml page. First, I create the button

<input type="button" id="myButton" onclick="Save()"/>  

And I have a function on the same page

<script> function Save() { ..do stuff here } </script>

This function never gets called. What am I doing wrong?

Upvotes: 3

Views: 5499

Answers (1)

Divyang Desai
Divyang Desai

Reputation: 7866

Here you can do like this way:

$(document).ready(function() {
      $("#myButton").click(function() {
        alert("button clicked.");
      });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="button" id="myButton" value="test"/>

Upvotes: 4

Related Questions