Link to home
Start Free TrialLog in
Avatar of heppa
heppaFlag for Luxembourg

asked on

onSubmit Event while using document.form.submit()

Hi Everybody!

I have a problem concerning the onSubmit event inside forms. when i have a submit button or image (<input type="submit"> or <input type="image">) the event is called. when i use a hyperlink (<a href="javascript:document.testform.submit()"...>) the form is being submitted, but the event is not called.

Working Code (reduced):

<form name="testform" method="POST" action="test.php" onsubmit="return chkForm();">
<input type="submit" value="abschicken">
</form>

not working code (reduced):

<form name="testform" method="POST" action="test.php" onsubmit="return chkForm();">
<a href="javascript:document.testform.submit();" >send</a>
</form>

Any Ideas how i can send the form with a hyperlink WITH onSubmit event being raised?

Any Help would be greatly appreciated.
Thx in advance.

Kind Regards.
Alex Hepp
Avatar of cLFlaVA
cLFlaVA


Two methods to try:

<a href="document.testform.submit(); return false;" >send</a>

And:

<a href="#" onclick="document.testform.submit(); return false;" >send</a>
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heppa

ASKER

Thank you for your efforts...

cLFlaVA, your versions don´t work either. The first one does nothing because of the return false; and the second one also submits the form, but the event onsubmit is not being raised.

Zvonko: Your approach does work, but is that really the only workaround to get this to work? I don´t really like such "hidden features" ;) but thank you very much. At least a workaround;) Will accept your answer later, after one or two days. Perhaps there is a more standardlike way...

Thank you!
Do take into account that Zvonko is at the top of the list in the JavaScript hall of fame, as well as being the featured JavaScript expert.  I think your answer lies in safe hands.
Is this method more "standard"  to you:

<a href="#" onClick="f=document.testform;if(f.onsubmit())f.submit();return false;" >send</a>

Avatar of heppa

ASKER

I know that, and i really appreciate it very much. Perhaps my comment was a bit lacking the respect, the workaround deserves. It´s great!!!
But nevertheless it is a workaround, and i think there´s a kind of bug in javascript. When i call the method submit, it should be the same as clicking a submit button. or am i wrong?

Kind Regards.
cLFlaVA, do you really believe I know everything?
This still experts Exchange where I do learn every day.
Avatar of heppa

ASKER

At Zvonko:

;)

Nope, you´re right. I hope, you do not misunderstand my words... The thing about workaround is more going towards javascript then you... i excuse for my too short comment.

Kind regards.
heppa, in my last hyperlink you see that there is a difference betwean forms onsubmit() event handler and forms submit() method.
all i was doing was expressing my confidence in the answer.  i do not really believe you know everything :)
Sorry cLFlaVA, but I react to such compares because when I started in this topic area I was asked to be more respectful to "honored" experts comments. I was not too respectful, so I hope you are also not too respectful ;-)
Avatar of heppa

ASKER

ok, i see. But in my eyes, calling the method submit(), i want the form to be submitted. And when the form is being submitted, the event onSubmit should be raised...

Perhaps only my special meaning, and totally off the green, but i know it now, and i know, that i can´t change javascripts behaviour, so i will accept it, and work with it.

Thank you very much for making that clear...

I did not only put confidence in the answer, i checked it out, but you must agree, that a hidden submit button, and a link, that simulates a click on this button, is a workaround, even when it is a really great one, that i would have never discovered.

Thanks
heppa,
Did you try Zvonko's latter suggestion?  It takes into account the fact that [1] programmatic calls to a form's submit() method do bypass the onsubmit event (yes, this is normal and expected, although I don't know what the reasoning behind it is), and [2] you can generally call the form's onsubmit code yourself as though onsubmit were simply another method of the form object.
I think you see the solution, but only to go for sure, in the last hyperlink you do not need the hidden submit button.
Of course you are true, and also I have expected the onSubmit event to be fired when form is submitted.
But see it like this: when you have a form field with an onChange event is that event also not fired when field value is set by script.

That's the way how it works and we have to live with it.
So, why exactly do you need to use a hyperlink?
Avatar of heppa

ASKER

because design is hyperlink, not button, simple as that...

@Zvonko: You are right, we have to live with it. but i would say, that the onchange event should also be fired, when value is changed by script... nevertheless, i think, this is being discussed enough.

Thank you for your help
You are welcome.