Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form and observe the onsubmit event

This should be trivial, but I am not getting it :\

HTML

<form id="myform" action="#">
    <input type="submit" value="Submit" />
    <a href="#" onclick="$('myform').submit()">Submit</a>
</form>  

JS (Prototype)

$('myform').observe('submit', function(e) { 
    alert('submitted!');
    e.stop();
});

Why the submit is only triggered with the submit via input? Shouldn't the submit event be triggered with $('myform').submit() also?

JSFiddle: http://jsfiddle.net/d8BdM/

like image 712
Rui Carneiro Avatar asked Jan 18 '26 11:01

Rui Carneiro


1 Answers

Change the form like this:

<form id="myform" action="">
<input type="submit" value="Submit" id="sub_but" />
<a href="#" onclick="$('sub_but').click();">Submit</a>
</form>

And keep the other code; i tested in your fiddle and works.

Saludos ;)

like image 96
Hackerman Avatar answered Jan 21 '26 00:01

Hackerman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!