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
-
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?ewitkows– ewitkows2016年08月16日 12:59:17 +00:00Commented Aug 16, 2016 at 12:59
7 Answers 7
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>
Comments
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;
Comments
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" />
Comments
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');
});
});
Comments
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');
}
Comments
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" />
Comments
function btnclick() {
alert('You Clicked Button');
};
<input type="button" id="btn_click" value="Click" onclick="btnclick();" />
Comments
Explore related questions
See similar questions with these tags.