View Full Version : submit() = onsubmit?

03-18-2005, 05:39 AM
i have this js form vaildator.
in the <form> tag it has an onsubmit="....." part. It works the way its supposed to.

now, theres this other part to the form. i have a "preview" button which brings up a confirm() with the values of the inputs in the form.

So after filling out the inputs, you hit Preview. It brings up a confirm() with the values that you just typed. Since it's confirm(), theres Ok and Cancel. If you hit OK, it will send the form (.....submit()).

But when you do that, the first form vaildator (which uses the onsubmit="") doesn't check the confirm one.

what do i do?

03-18-2005, 05:57 AM
The form submit() method doesn't trigger the onsubmit event. I don't know why it doesn't when it should. The solution is to make the preview button a submit button.

<input type="submit" name="btnPreview" value="Preview" onclick="return confirm('Are you sure you want to preview the page?');" />

03-18-2005, 10:13 AM
Another way would be to call the function specified in the onclick event manually before using the submit() function, or you could use dispatchEvent to simulate a real click on the submit button.

03-19-2005, 02:52 AM
glenngv, if i make the other buton (the preview one) into a submit also (type="submit"), wouldn't that interfere with the real submit button?

so this is my preview button's code:

<input type="submit" value="Preview" onclick="preview();" />

and preview() is:

function preview() {
name = document.getElementById("name").value;
email = document.getElementById("email").value;
url = document.getElementById("url").value;
subject = document.getElementById("subject").value;
msg = document.getElementById("message").value;

if ((msg) && (confirm("Message Preview-\n\nName:\n"+name+"\n_______________\nEmail:\n"+email+"\n_______________\nWebsite:\n"+url+"\n_______________\nSubject:\n"+subject+"\n_______________\nMessage:\n"+msg+"\n_______________\nSend?"))) {
else {
alert("Type in your message before previewing.");


and martijntje, i dont understand your method (im new to js). ill probably stick with glenngv's, but thanks anyway.

03-21-2005, 05:14 AM
Make the preview() function return true or false.

<input type="submit" value="Preview" onclick="return preview();" />
function preview() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var url = document.getElementById("url").value;
var subject = document.getElementById("subject").value;
var msg = document.getElementById("message").value;

if (!msg){
alert("Type in your message before previewing.");
return false;
else {
return confirm("Message Preview-\n\nName:\n"+name+"\n_______________\nEmail:\n"+email+"\n_______________\nWebsite:\n"+url+"\n_______________\nSubject:\n"+subject+"\n_______________\nMessage:\n"+msg+"\n_______________\nSend?");
When false is returned, the form submission is cancelled, otherwise, submission continues and triggers onsubmit handler.