Javascript confirm and disable button

webressurs
webressurs used Ask the Experts™
on
I have a javascript that display a confirm dialogue when the button is clicked if a checkbox is checked. The javascript works perefect in all browsers.


<asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="javascript:return confirmDelete();" />


<script type="text/javascript">
function confirmDelete()
{
    Page_ClientValidate();
    if (Page_IsValid)
    {
        var chkDelete = document.getElementById("<%= chkDelete.ClientID  %>");
        if (chkDelete)
        {
            if(chkDelete.checked)
            {
                return confirm("Are you sure?");
            }
        }
    }
    return Page_IsValid;
}
</script>


Now, I want to call another javascript when OnClientClick occures. The script disables the button to prevent double inserts if a user doubleclicks the button. The script works perfect and is like this:


<asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="disableButton(this, '');" />


<script language="javascript" type="text/javascript">
  function disableButton(sender, group)
  {
    Page_ClientValidate(group);
    if (Page_IsValid)
    {
        sender.disabled = "disabled";
        __doPostBack(sender.name, '');
    }
  }
</script>


The question is: How can I call the javascript disableButton(this, ''); from confirmDelete()? If I do like this the form is not submitted at all:


<asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="javascript:return confirmDelete();" />


<script type="text/javascript">
function confirmDelete()
{
    Page_ClientValidate();
    if (Page_IsValid)
    {
        var chkDelete = document.getElementById("<%= chkDelete.ClientID  %>");
        if (chkDelete)
        {
            if(chkDelete.checked)
            {
                return confirm("Are you sure?");
            }

             disableButton(this, '');

        }
    }
    return Page_IsValid;
}
</script>


I guess the problem is that confirmDelete() use "return"?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
have you tried something like this?

if(chkDelete.checked)
{
    if ( confirm("Are you sure?") )
    {
           disableButton(this, '');
    }
}

Author

Commented:
Hi! This don't work. I only want to disable the button if chkDelete.checked = false. If chkDelete.checked = true I want to display the confirm dialogue.

Something like this (not working):


<asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="javascript:return confirmDelete();" />


<script type="text/javascript">
function confirmDelete()
{
    Page_ClientValidate();
    if (Page_IsValid)
    {
        var chkDelete = document.getElementById("<%= chkDelete.ClientID  %>");
        if (chkDelete)
        {
            if(chkDelete.checked)
            {
                return confirm("Are you sure?");
            }
            else
            {
                disableButton(this, '');
            }
        }
    }
    return Page_IsValid;
}
</script>
I solved this, here is the solution:

<asp:Button runat="server" ID="btnSave" Text="Save" OnClientClick="javascript:return confirmDelete(this);" CausesValidation="true" />

<script type="text/javascript">
function confirmDelete(sender)
{
    Page_ClientValidate();
    if (Page_IsValid)
    {
        var chkDelete = document.getElementById("<%= chkDelete.ClientID  %>");
        if (chkDelete)
        {
            if(chkDelete.checked)
            {
                return confirm("Vennligst bekreft at du ønsker å slette innlegget med tilhørende kommentarer. Det er ikke mulig å angre dette!");
            }
            else
            {
                disableButton(sender, '');
            }
        }
        else
        {
            disableButton(sender, '');
        }
    }
    return Page_IsValid;
}
</script>

Author

Commented:
This is working perfect.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial