<< Previous Next >>
Tutorial home...

JavaScript confirmation box example #2

Input Data:

Enable confirmations:


In this example we have defined our own function called myConfirm(). Take a look at the source for this page to see how the JavaScript code is embedded in the HEAD section of the HTML file.

The JavaScript code is as follows:-

// The following function displays the prompt passed as a parameter in a confirmation dialog
// returning either true or false to represent the user clicking yes or no.
function myConfirm(aPrompt)
{
	if (document.myform.enable_confirmations.checked)
	{
		return confirm(aPrompt);
	}
	else 
	{
		return true;
	}
}

What this code does is evaluates the checked attribute of the CHECKBOX object which is incorporated into the form. If it is true it presents the confirmation dialog, otherwise it will return true, resulting in the form being submitted immediately.

The ONSUBMIT event handler calls our function in just the same way as it did the standard confirm() function:-

return myConfirm('Are you sure you want to submit this data?')


<< Previous Next >>
Tutorial home...