jQuery event fires on doc ready

Posted by gmcalab on Stack Overflow See other posts from Stack Overflow or by gmcalab
Published on 2010-04-06T18:39:38Z Indexed on 2010/04/06 18:43 UTC
Read the original article Hit count: 222

Filed under:

I am trying to set the click event of a button on my form and for some reason I am getting weird behavior. When I bind the click event to a function that takes no arguments, things seem to work fine. But when I bind the event with a function that takes an argument, the event fires on document ready and on click. Any ideas?

Example 1:

This causes an alert box to fire on ready and when the button is clicked.

jQuery(document).ready(function(){
   $('myButton').click(alert('foo'));
});

Example 2:

This causes an alert box to fire ONLY when the button is clicked.

jQuery(document).ready(function(){
   $('myButton').click(wrapper);
});

// External js file
function wrapper(){
   alert('bar');
}

© Stack Overflow or respective owner

Related posts about jQuery