Question

I am trying to submit form data, but instead of using the standard "Submit" button I want to use a regular HTML link. However, when I click this link I want to have a confirmation box asking the user to continue or cancel. I have both parts of the puzzle, but when I put them together they don't play nice. This is what I currently have for Javascript code:

function confirmSubmit()
{
var agree=confirm("Are you sure you wish to delete this file?");
if (agree)
    return true ;
else
    return false ;
}
function submitform() {
    document.forms["delete14"].submit();
}

And for the HTML I have:

<a href="javascript:submitform()" onClick="javascript:confirmSubmit()">link text</a>

The confirmation box does pop up as intended and the form does submit properly. But currently the form submits even if the user clicks "no" on the confirmation box.

Was it helpful?

Solution

Try this:-

function confirmSubmit()
{
var agree=confirm("Are you sure you wish to delete this file?");
if (agree)
    document.forms["delete14"].submit();
else
    return false ;
}

OTHER TIPS

Firstly, don't use a link if you don't want one, use a styled span (or a button, which says to the user "click me and something will happen" rather than a link, which says "click me and I'll take you to another page").

Also, don't use "javascript:" in an attribute that expects a value that is script, it will be seen as a useless label.

Anyhow, your issue is that you have two listeners on the link:

<a href="javascript:submitform()" onClick="javascript:confirmSubmit()">link text</a>

The one in the href will submit the form unconditionally. So use a span and get rid of the href:

<span style="cursor: pointer; text-decoration: underline"
   onclick="confirmSubmit()">link text</span>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top