tags:

views:

44

answers:

3

How do I write OnClick="return Foo()" in jquery to prevent the button from submitting while the function is not yet finished. TY

+1  A: 

It will not submit until function has finished executing anyway. If you want to prevent the button from submitting just return false from Foo().

HeavyWave
+2  A: 

Attach a handler to the click event for your target element, pass your handler the event object as a parameter, and call preventDefault() on the event within your handler. Like so:

$('#mydiv').click(function(e) {
  // do something
  e.preventDefault();
});

For more information see click and the event object at the jQuery API.

Jimmy Cuadra
A: 

Check out the jQuery docs for click. Something like this perhaps?

$('#buttonid').click(function(evt) {
    evt.preventDefault();
    Foo();
});

Keep in mind that the click event is triggered for submit buttons/inputs even when selected through a keyboard event.

jQuery has excellent documentation. Take some time to read through it and you'll get where you want faster than guess work.

Peter Gibson