156

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?

<!-- Begin Button -->  
<div class="demo">
<br> <br> <br>   
<input id = "btnSubmit" type="submit" value="Release"/>
<br> <br> <br>  
</div>
<!-- End Button -->

And in the javascript file

function btnClick()
{
    //    button click
    $("#btnSubmit").button().click(function(){
        alert("button");
    });    
}
Ahmad Farid
  • 13,900
  • 45
  • 94
  • 135

9 Answers9

285

You have to put the event handler in the $(document).ready() event:

$(document).ready(function() {
    $("#btnSubmit").click(function(){
        alert("button");
    }); 
});
Davide Gualano
  • 12,513
  • 9
  • 46
  • 65
28
$(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;
     });


});
Charles Caldwell
  • 15,849
  • 4
  • 39
  • 47
Lawrence Gandhar
  • 688
  • 8
  • 12
  • 5
    Good because is the onlyone who insert the preventDefault() – Gabrer Dec 20 '14 at 11:16
  • 13
    As of jQuery 1.7, the `.on()` method is the preferred method for attaching event handlers to a document. Therefore this would be better: `$('your selector').on("click", ...);` – Tinynumbers Sep 02 '15 at 14:23
21
$('#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/

Pang
  • 9,073
  • 146
  • 84
  • 117
10

Try This:

$(document).on('click', '#btnClick', function(){ 
    alert("button is clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <button id="btnClick">Click me</button> 
Rafiqul Islam
  • 853
  • 11
  • 14
8
 $("#btnSubmit").click(function(){
        alert("button");
    });    
Ali Tarhini
  • 5,158
  • 6
  • 40
  • 64
3
<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">
Nivediny
  • 31
  • 1
2

<script type="text/javascript">

    $(document).ready(function() {

    $("#Button1").click(function() {

        alert("hello");

    });

    }
    );

</script>
1
$('#btnSubmit').click(function(event){
    alert("Button Clicked");
});

or as you are using submit button so you can write your code in form's validate event like

$('#myForm').validate(function(){
    alert("Hello World!!");
});
Charles Caldwell
  • 15,849
  • 4
  • 39
  • 47
Hiren
  • 57
  • 1
  • 11
0

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>
navyad
  • 3,533
  • 6
  • 43
  • 78