...

View Full Version : submit() = onsubmit?



qwertyuiop
03-18-2005, 04: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?

glenngv
03-18-2005, 04: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?');" />

martijntje
03-18-2005, 09: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.

qwertyuiop
03-19-2005, 01: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?"))) {
document.getElementById("form").submit();
}
else {
alert("Type in your message before previewing.");
}
}


thanks

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

glenngv
03-21-2005, 04: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum