10

I am trying to get the click event of a submit button on my form...

<input type="submit" value="Search By Demographics" class="button" id="submitDemo"/>

That is the button that I want to get the event from.

$('#submitDemo').click(
        alert("work darn it!!!!!!!!!!!!!!!!!!!!!!!!!")
        //$("#list").block({ message: '<img src="../../Images/ajax-loader.gif" />' })
    );

This is what I want to do on button click. I want to put a loading image around a div and then unblock later in a different function. Currently the overlay pops up when the page loads instead of onClick. What am I doing

SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195

6 Answers6

14

try this

$('#submitDemo').live("click",function() {
  // your stuff
});

As @px5x2 pointed that live is deprecate so instead of live use ON

$('#submitDemo').on("click",function() {
  // your stuff
}); 
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
8
$('#submitDemo').click(function() {
  // do your stuff
});
Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
4

You need to run your script when the DOM is ready, as well as providing a callback to the click method:

$(function() {
    $('#submitDemo').click(function() {
        // do your stuff
    });
});
Alex
  • 34,899
  • 5
  • 77
  • 90
3

You have to encase your code in a function..

$('#submitDemo').click(function(){
    alert("work darn it!!!!!!!!!!!!!!!!!!!!!!!!!")
    //$("#list").block({ message: '<img src="../../Images/ajax-loader.gif" />' })
});

But it is always a good practice to bind the event using on so that it attaches the event handler to the element when that is available.. So

$('#submitDemo').on('click',function(){
    alert("work darn it!!!!!!!!!!!!!!!!!!!!!!!!!")
    //$("#list").block({ message: '<img src="../../Images/ajax-loader.gif" />' })
});
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
2

For form you can also bind submit button click on form.

$("#form_selector").submit(function{
// Do your stuff
})

where 'form_selector' is id of the form of which submit button is clicked.

Paritosh Singh
  • 6,288
  • 5
  • 37
  • 56
0

You might haven't included the jquery library in your code!

src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
Bariock
  • 51
  • 7