You are here: irt.org | FAQ | JavaScript | Form | Q1713 [ previous next ]
You either need to amend the form's action attribute, as described here:
<html>
<head>
<script language="JavaScript"><!--
function updateAction(form) {
if (form.whereTo.checked)
form.action = 'mailto:someone@somewhere.com';
else
form.action = 'http://somewhere.com/somepage.htm';
}
//--></script>
</head>
<body>
<form onSubmit="updateAction(this)">
Email: <input type="checkbox" name="whereTo">
</form>
</body>
</html>Or, for those older browsers that only allow read access to the action attribute, copy the data in the visible form to a hidden form with a different action attribute value and then submit that one and cancel the submission of the current form:
<html>
<head>
<script language="JavaScript"><!--
function checkAction(form) {
if (form.whereTo.checked) {
document.forms.hiddenForm.somedata.value = form.somedata.value;
document.forms.hiddenForm.submit();
return false;
}
return true;
}
//--></script>
</head>
<body>
<form action="http://www.somewhere.com/somepage.htm" onSubmit="return checkAction(this)">
<input type="text" name="somedata">
Email: <input type="checkbox" name="whereTo">
</form>
<form name="hiddenForm" action="mailto:someone@somwhere.com">
<input type="text" name="somedata">
</form>
</body>
</html>