Rui Carneiro
Rui Carneiro

Reputation: 5671

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/

Upvotes: 3

Views: 5344

Answers (2)

EpokK
EpokK

Reputation: 38092

Change your javascript code like this:

$('myform').onsubmit = function() {
    alert('submitted!');
    return false;
};

Upvotes: 2

Hackerman
Hackerman

Reputation: 12295

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 ;)

Upvotes: 2

Related Questions