Forum: Rails Spinoffs (closed, excessive spam) form.submit() not firing onSubmit calls

Posted by louis w (Guest)
on 2008-07-01 17:29
(Received via mailing list)
I have a form which contains widgEditor (http://www.themaninblue.com/
experiment/widgEditor/) elements. This script adds onsubmit calls to
the form to clean itself up before submitting values.

The problem arises when I try to submit the form through javascipt
with $(form).submit();. When this happens it does not fire any of the
onSubmit functions which widg added. This works fine if i submit the
form through a normal submit button.

This is the code in widg (I do not want to change this, just for
reference).
theForm.observe('submit', function(e) {

How can I get my JS to execute the onsubmit actions?
Posted by Frederick Polgardy (Guest)
on 2008-07-01 17:32
(Received via mailing list)
That is correct, submit observers are not called when you call
form.submit().  If you need to be able to call form.submit() as well as 
trap
the user submission mechanism, you need to factor out your code into
something you can call from both places.

-Fred

On Tue, Jul 1, 2008 at 10:28 AM, louis w <louiswalch@gmail.com> wrote:

>
> I have a form which contains widgEditor (http://www.themaninblue.com/
> experiment/widgEditor/<http://www.themaninblue.com/experiment/widgEditor/>)
> elements. This script adds onsubmit calls to
> the form to clean itself up before submitting values.
>
> The problem arises when I try to submit the form through javascipt
> with $(form).submit();. When this happens it does not fire any of the
> onSubmit functions which widg added. This works fine if i submit the
> form through a normal submit button.


--
Science answers questions; philosophy questions answers.
Posted by louis w (Guest)
on 2008-07-01 17:39
(Received via mailing list)
Thanks Fred.
Do you know of a way to submit a form through js and have it honor the
observers?
I would prefer not to alter the 3rd party code. This could be
problematic for future upgrades.
Posted by Walter Davis (walterdavis)
on 2008-07-01 17:42
(Received via mailing list)
Try sending the form with $('submit_button').click() -- new-school
meets old-school.

Walter
Posted by Frederick Polgardy (Guest)
on 2008-07-01 17:43
(Received via mailing list)
I'd guess you could fire the event with the browser Event API 
(element.fire
in Prototype tries to iron out the browser issues), but I'm not sure, I
haven't tried it.

On Tue, Jul 1, 2008 at 10:39 AM, louis w <louiswalch@gmail.com> wrote:

>
> Thanks Fred.
> Do you know of a way to submit a form through js and have it honor the
> observers?
> I would prefer not to alter the 3rd party code. This could be
> problematic for future upgrades.


--
Science answers questions; philosophy questions answers.
Posted by louis w (Guest)
on 2008-07-01 18:10
(Received via mailing list)
Nice one. .click() works. Thanks.
Posted by ColinFine (Guest)
on 2008-07-02 10:52
(Received via mailing list)
On Jul 1, 4:43 pm, "Frederick Polgardy" <f...@polgardy.com> wrote:
> I'd guess you could fire the event with the browser Event API (element.fire
> in Prototype tries to iron out the browser issues), but I'm not sure, I
> haven't tried it.
>
No, Element#fire is specifically only for custom events, not native
ones.
Posted by Justin Perkins (Guest)
on 2008-07-02 17:18
(Received via mailing list)
On Tue, Jul 1, 2008 at 11:09 AM, louis w <louiswalch@gmail.com> wrote:
> Nice one. .click() works. Thanks.

If you have an inline JavaScript handler for the submit event, then
you can fire it by just calling onsubmit like a method.

Given:

<form onsubmit="doStuff()" id="my-form">
...
</form>


$('my-form').onsubmit()

Think of it as a named method just waiting to be invoked.

-justin
Posted by Frederick Polgardy (Guest)
on 2008-07-02 17:21
(Received via mailing list)
Right, you just can't do this if you've registered it with observe().

-Fred

On Jul 2, 2008, at 10:17 AM, Justin Perkins wrote:

> ...
> </form>
>
>
> $('my-form').onsubmit()
>
> Think of it as a named method just waiting to be invoked.
>
> -justin

--
Science answers questions; philosophy questions answers.
This topic is locked and can not be replied to.