2

In Mvc Javascript Is Not Working Button Click Event I Want To Raise Button Click Event.

<input type="button" id="btn_click" value="Click" />
<script type="text/javascript">
 function pageLoad() {
 $('#btn_click').click(function () {
 alert('You Clicked Button');
 });
 }
</script>

Please Help me

ekad
14.7k26 gold badges46 silver badges49 bronze badges
asked Aug 16, 2016 at 12:09
1
  • did you include appropriate jquery files? Also, hit F12 for browser tools, reload the page, and try clicking the button again, does the console report any errors? Commented Aug 16, 2016 at 12:59

7 Answers 7

3

This actually has nothing to do with ASP.NET, C# or Razor. This is pure HTML and JavaScript. You have wrapped the click function in pageLoad which is not being called. So you simply have to remove it.

<html>
<head>
 <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
</head>
<body>
 <input type="button" id="btn_click" value="Click" />
 <script type="text/javascript">
 $('#btn_click').click(function() {
 alert('You Clicked Button');
 });
 </script>
</body>
</html>

answered Aug 16, 2016 at 13:07

Comments

1

For button

<input type="button" id="btn_click" value="Click" />

Use javascript as-

function showalert(){
 alert('You Clicked Button');
} 
document.getElementById("btn_click").onclick = showalert;
answered Aug 16, 2016 at 14:02

Comments

0

pageLoad function is never called hence event-listener is never attached!

Either invoke it or just Place your <script> as last-child of <body>(Just before closing body tag(</body>))

$('#btn_click').click(function() {
 alert('You Clicked Button');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="button" id="btn_click" value="Click" />

answered Aug 16, 2016 at 12:11

Comments

0

pageLoad function not call, but if you want to call it after pageload you can use $(function(){}) like that

$(function(){
 $('#btn_click').click(function () {
 alert('You Clicked Button');
 });
});
answered Aug 16, 2016 at 12:14

Comments

0

You have wra the click event in a function , but button click does not call that function.

You can call this function onClick event of button

<input type="button" id="btn_click" value="Click" onClick="pageLoad()" />
function pageLoad() {
 alert('You Clicked Button');
}

JSFIDDLE

answered Aug 16, 2016 at 12:14

Comments

0

Put your Javascript code in document ready.

$(document).ready(function () { 
 $('#btn_click').click(function () {
 alert('You Clicked Button');
 });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="btn_click" value="Click" />

answered Aug 16, 2016 at 13:08

Comments

0

function btnclick() {
 alert('You Clicked Button');
};
 <input type="button" id="btn_click" value="Click" onclick="btnclick();" />

answered Nov 6, 2017 at 3:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.