164

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
  • 14,398
  • 45
  • 96
  • 136

9 Answers9

302

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

$(document).ready(function() {
    $("#btnSubmit").click(function(){
        alert("button");
    }); 
});
Davide Gualano
  • 12,813
  • 9
  • 44
  • 65
32
$('#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,564
  • 146
  • 81
  • 122
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
  • 16,649
  • 4
  • 40
  • 47
Lawrence Gandhar
  • 718
  • 8
  • 12
  • 5
    Good because is the onlyone who insert the preventDefault() – Gabrer Dec 20 '14 at 11:16
  • 14
    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
13

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
  • 931
  • 11
  • 14
  • 1
    This version has the benefit of still working if #btnClick is created after this script runs. – Jimbali Apr 27 '20 at 20:41
9
 $("#btnSubmit").click(function(){
        alert("button");
    });    
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
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
1

<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
  • 16,649
  • 4
  • 40
  • 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,752
  • 7
  • 47
  • 88