I need to have a button and handle its event in jQuery. And I am writing this code but it\'snot working. Did I miss something?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function(){
alert("Hello");
});
});
</script>
<input type="button" id="button" value="Click me">
You have to put the event handler in the $(document).ready() event:
$(document).ready(function() {
$("#btnSubmit").click(function(){
alert("button");
});
});
Works for me
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$("#btn").click(function() {
alert("email")
});
</script>
$(document).ready(function(){
$('your selector').bind("click",function(){
// your statements;
});
// you can use the above or the one shown below
$('your selector').click(function(e){
e.preventDefault();
// your statements;
});
});
$('#btnSubmit').click(function(){
alert("button");
});
or
//Use this code if button is appended in the DOM
$(document).on('click','#btnSubmit',function(){
alert("button");
});
See documentation for more information:
https://api.jquery.com/click/
<script type="text/javascript">
$(document).ready(function() {
$("#Button1").click(function() {
alert("hello");
});
}
);
</script>