0

I have a situation where I need to open up a confirmation dialog as in the image, on the click of Save Update button.

The button needs to check the value of the checkbox, if checked,only then open the confirmation dialog.

To achieve this, I have added an onchange event to the field which sets a global JS script variable (checkfield)

The onclick event then checks the variable checkfield and opens up the dialog.

enter image description here

The button goes like this:

<apex:inputCheckbox id="investigation" onchange="checkCaseStatus('{!$Component.investigation}')" value="{!underInvestigation}" label="{!fieldMap2[field]}" style="float:left;width:10%">

<apex:commandButton value="Save Update" action="{!showEmailTemplate}" onclick="checkandSubmit()">

//JS function

<script>
var checkfield = true;
  function checkCaseStatus(check){
   checkfield =  document.getElementById(check).checked; debugger;
   }

 function checkandSubmit(){
    if(checkfield == true)// I am able to get the checkbox value, the dialog opens
        {
            if(!confirm('Are you sure?'))
            {
                //Cancel Action
                return false;// i am returning false on cancel , because i dont want to execute the apex action="{!showEmailTemplate}"
            } else {return true;}// I want to execute the action, hence true

        }

   }

But in both the cases(OK and Cancel) the apex action is executed.

5
  • Please share the rest of your javascript, it looks like you're missing a couple of functions. Apr 20, 2020 at 11:52
  • My apologies, the function checkandSubmit() is the onlclick event JS fucntion , which checks for the value set by another JS function checkCaseStatus. Apr 20, 2020 at 11:55
  • I think your onclick needs to have a return statement, rather than just a function call, so: onclick="return checkandSubmit();" Apr 20, 2020 at 12:00
  • @rael_kid Hi Mate, I have tried your suggestion , but the apex controller action doesnt get executed. Apr 20, 2020 at 12:10
  • <apex:commandButton value="Save Update" action="{!showEmailTemplate}" onclick="checkandSubmit()"> Apr 20, 2020 at 12:10

1 Answer 1

0

I have refactored the onclick event, it solved the problem. thanks to all for your suggestions

onclick="if(checkfield == false && !confirm('Are you sure?')){return false;}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .