function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DavidGenDavidGen 

I can not save changes on checkboxes

Hi, 

 

I'm creagint a VF with ten input elements of type checkbox. I'm adding a javascript that creates dependencies between checkboxes. This work well and automatic checks are visible, but when I press save button, these changes are not saved. Any Solution?

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
DavidGenDavidGen

I just discovered that you can not get the value of a disabled field . Thanks anyway.

All Answers

DavidGenDavidGen

My VF's sample code (3 checkboxes):

 <apex:page standardController="object__c" extensions="newObject" id="j_id0">

 

 <apex:pageBlockSection title="Checkboxes" id="Block2" columns="2">

 <apex:inputField id="CIA_CHECK1__c" value="{!object.CIA_CHECK1__c}"/>
 <apex:inputField id="CIA_CHECK2__c" value="{!object.CIA_CHECK2__c}"/>
 <apex:inputField id="CIA_CHECK3__c" value="{!object.CIA_CHECK3__c}"/>

</apex:pageBlockSection>

 

<script languaje="javascript">
      {

          var checkbox1= "j_id0:Block2:CIA_CHECK1__c";

          var checkbox2= "j_id0:Block2:CIA_CHECK2__c";

          var checkbox3= "j_id0:Block2:CIA_CHECK3__c";

 

        document.getElementById(checkbox1).onchange = function(evt){ update_checks()};

        document.getElementById(checkbox2).onchange = function(evt){ update_checks()};

        document.getElementById(checkbox3).onchange = function(evt){ update_checks()};

 

        function enable(id){
          document.getElementById(id).disabled = false;  
        }
        function disable(id){
          document.getElementById(id).disabled = true;
        }
        function check(id){
          document.getElementById(id).checked = true;
          document.getElementById(id).value = 1;
        }
        function uncheck(id){
          document.getElementById(id).checked = false;
          document.getElementById(id).value = 0;
        }

 

        function enableAll(){
          enable(checkbox1);
          enable(checkbox2);
          enable(checkbox3);

         }

 

    function update_checks(){

     enableAll();
          if (document.getElementById(checkbox1).checked === true){
            disable(checkbox2);
            disable(checkbox3);
                        
            uncheck(checkbox2);
            check(checkbox3);
          }

          if(document.getElementById(checkbox2).checked === true){
            disable(checkbox1);
            disable(checkbox3);
                        
            uncheck(checkbox3);
            check(checkbox1);
          }

 

         update_checks();
      }
    </script>
</apex:page>

__

<script languaje="javascript"> ?

DavidGenDavidGen

I already know that <script type="text/javascript"> is now the correct form but, because language="javascript" is deprecated does not mean that it will not work.


Thanks.

__

If you can explain me the use case, I might be able to come up with a better solution - this looks like awfully lots of javscript...

DavidGenDavidGen

You're right. But right now my only problem is that when I activate checkbox1, javascript must be enabled checkbox3 e.g. However when I push "save" button, I look record and no checkbox is enabled.

 

__
 <apex:page standardController="object__c" extensions="newObject" id="j_id0">

 

 <apex:pageBlockSection title="Checkboxes" id="Block2" columns="2">

 <apex:inputField id="CIA_CHECK1__c" value="{!object.CIA_CHECK1__c}" onselect="update_checks()"/>
 <apex:inputField id="CIA_CHECK2__c" value="{!object.CIA_CHECK2__c}" onselect="update_checks()"/>
 <apex:inputField id="CIA_CHECK3__c" value="{!object.CIA_CHECK3__c}" onselect="update_checks()"/>

</apex:pageBlockSection>

 

<script languaje="javascript">
      {

          var checkbox1= "j_id0:Block2:CIA_CHECK1__c";

          var checkbox2= "j_id0:Block2:CIA_CHECK2__c";

          var checkbox3= "j_id0:Block2:CIA_CHECK3__c";


        function enable(id){
          document.getElementById(id).disabled = false;  
        }
        function disable(id){
          document.getElementById(id).disabled = true;
        }
        function check(id){
          document.getElementById(id).checked = true;
          document.getElementById(id).value = 1;
        }
        function uncheck(id){
          document.getElementById(id).checked = false;
          document.getElementById(id).value = 0;
        }

 

        function enableAll(){
          enable(checkbox1);
          enable(checkbox2);
          enable(checkbox3);

         }

 

    function update_checks(){

     enableAll();
          if (document.getElementById(checkbox1).checked === true){
            disable(checkbox2);
            disable(checkbox3);
                        
            uncheck(checkbox2);
            check(checkbox3);
          }

          if(document.getElementById(checkbox2).checked === true){
            disable(checkbox1);
            disable(checkbox3);
                        
            uncheck(checkbox3);
            check(checkbox1);
          }

         // not sure why you call update_checks() again.... 

         update_checks();
      }
    </script>
</apex:page>

See code... overall, have you tried to open page in firefox and see what webdeveloper panel says? ctrl+shft+K

aballardaballard

Maybe you are getting some kind of a validation error on save?   Try including <apex:messages/> at the top of the page to make sure any error messages are displayed. 

 

DavidGenDavidGen

I just discovered that you can not get the value of a disabled field . Thanks anyway.

This was selected as the best answer