jQuery event fires on doc ready
- by gmcalab
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');
}