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
vikrant kumarvikrant kumar 

Autocheck checkbox when other check box is checked in pageblocktable?

I have two checkboxes  in a row in pageblocktable.If i check one,the other should be automatically checked and if unchecked other checkbox should be unchecked.Dont know how to do it in pageblocktable.Please help me how to do this?
Best Answer chosen by vikrant kumar
Abhishek_DEOAbhishek_DEO
You may use global variable "$Component" to access apex component in javascript. More abount component
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_best_practices_accessing_id.htm

A sample code for your reference as per your requirement
 
<apex:page standardController="Account">
<script type="text/javascript">
  function makeSelect(myselection, cbid)
  {
    document.getElementById(cbid).checked = myselection.checked;
  }
</script>
<apex:form>
   <apex:pageBlock title="Hello {!$User.FirstName}!">
      You are viewing the {!account.name} account.
   </apex:pageBlock>
   <apex:pageBlock title="Contacts">
      <apex:pageBlockTable value="{!account.Contacts}" var="contact">
        <apex:column> <apex:inputCheckbox id="firstcheckbox" onclick="makeSelect(this,'{!$Component.secondcheckbox}')">FirstCheckbox </apex:inputCheckbox></apex:column>
        <apex:column> <apex:inputCheckbox id="secondcheckbox">secondcheckbox</apex:inputCheckbox>
         {!contact.name}</apex:column>
      </apex:pageBlockTable>
   </apex:pageBlock>
</apex:form>
</apex:page>

I am passing 2nd checkbox's id "secondcheckbox" to JS method on onClick event.To see the working of page you need to pass an accountid which have atleas 1 contact. for eg https://<serverURL>/apex/Checkbox?id=00120000018cZJw

Please mark it as best answer if it helps you. 

 

All Answers

Mathew Andresen 5Mathew Andresen 5
To do this without refreshes you would probably have to use javascript.  Something like an on change event that then updated the other checkbox.
Abhishek_DEOAbhishek_DEO
You may use global variable "$Component" to access apex component in javascript. More abount component
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_best_practices_accessing_id.htm

A sample code for your reference as per your requirement
 
<apex:page standardController="Account">
<script type="text/javascript">
  function makeSelect(myselection, cbid)
  {
    document.getElementById(cbid).checked = myselection.checked;
  }
</script>
<apex:form>
   <apex:pageBlock title="Hello {!$User.FirstName}!">
      You are viewing the {!account.name} account.
   </apex:pageBlock>
   <apex:pageBlock title="Contacts">
      <apex:pageBlockTable value="{!account.Contacts}" var="contact">
        <apex:column> <apex:inputCheckbox id="firstcheckbox" onclick="makeSelect(this,'{!$Component.secondcheckbox}')">FirstCheckbox </apex:inputCheckbox></apex:column>
        <apex:column> <apex:inputCheckbox id="secondcheckbox">secondcheckbox</apex:inputCheckbox>
         {!contact.name}</apex:column>
      </apex:pageBlockTable>
   </apex:pageBlock>
</apex:form>
</apex:page>

I am passing 2nd checkbox's id "secondcheckbox" to JS method on onClick event.To see the working of page you need to pass an accountid which have atleas 1 contact. for eg https://<serverURL>/apex/Checkbox?id=00120000018cZJw

Please mark it as best answer if it helps you. 

 
This was selected as the best answer
vikrant kumarvikrant kumar
Worked.Thank u very much!!