UI_Dev
UI_Dev

Reputation: 3427

javascript confirm function ok and cancel return same result

Here, I am calling confirm(param1, param2) method to display an alert Do you want to continue?

In this case, if the user clicks OK, it returns true, if the user clicks Cancel, it returns false.

But, when Cancel button is clicked, it is calling checkcount() method.

My requirement is whenever the user clicks on OK button, it should call checkcount() method.

How to achieve this?

<script>
function call(param1, param2, param3)
{
    if (condition)
    {
       /*some process */
    }
    else
    {
       confirm(param1, param2);
       checkcount();
    }
}

function confirm(param1, param2)
{
    if(condition)
    {
       var retVal = confirm("Do you want to continue ?");
       if (retVal == true)
       {
           alert("User wants to continue!");
           return true;
       } 
      else
      {
           alert("User does not want to continue!");
           return false;
      }
   }
}
</script> 

Upvotes: 1

Views: 2098

Answers (1)

epascarello
epascarello

Reputation: 207511

There is no check!

confirm(param1, param2);
checkcount();

and you want to change your function name to something other than confirm.

<script>
function call(param1, param2, param3)
{
    if (condition)
    {
       /*some process */
    }
    else
    {
        if(myConfirm(param1, param2)) {
            checkcount();
        }
    }
}

function myConfirm(param1, param2)
{
    if(condition)
    {
       var retVal = confirm("Do you want to continue ?");
       if (retVal == true)
       {
           alert("User wants to continue!");
           return true;
       } 
      else
      {
           alert("User does not want to continue!");
           return false;
      }
   }
}
</script> 

Upvotes: 2

Related Questions