0

I have a panel that display a text box and a check box. AIM1: I want to grayed out input field if check box is selected. And when page is loaded both should be open to use but once selected either one, other should clear its selection and gray out.

I tired Javascript/jquery nothing is working. I am not understanding why.

   <apex:panelGrid columns="5" id="theGrid" width="80%">
      <apex:outputLabel > A  </apex:outputLabel>
      <apex:inputfield id="idAText" value="{!ttlookup.a}" />
      <apex:inputCheckbox id="checkBoxAId" value="{!Acheckbox}" onchange="tt(this);"/>
      <apex:outputLabel >&nbsp; &nbsp;</apex:outputLabel>
      <apex:outputLabel > Check </apex:outputLabel>

      <apex:outputLabel >B  </apex:outputLabel>
      <apex:inputfield id="idRMText" value="{!ttlookup.B}" />
      <apex:inputCheckbox id="checkBoxBId" value="{!Bcheckbox}"/>
      <apex:outputLabel >&nbsp;&nbsp;</apex:outputLabel>
      <apex:outputLabel > Check </apex:outputLabel>

      <apex:outputLabel > B   </apex:outputLabel>
      <apex:inputfield id="idRMText" value="{!ttlookup.B}" />
      <apex:inputCheckbox id="checkBoxBId" value="{!Bcheckbox}"/>
      <apex:outputLabel >&nbsp; &nbsp;</apex:outputLabel>
      <apex:outputLabel > Check </apex:outputLabel>

    </apex:panelGrid> 
  <script type="text/javascript">

   function tt(chkboxCtrl) {

        var Aid = $("[id*=checkBoxAId]").is(':checked');
        alert(Aid);
        try {
                if( chkboxCtrl.checked ) {
                    $("[id*=idAText").style.display = 'none';
                }
                else {
                    $("[id*=idAText").style.display = 'inline';
                }
        }
        catch(e) {
            alert(e);
        }
    }

</script>

enter image description here

if i can change this

if( chkboxCtrl.checked ) {
     $("[id*=idAText").css("display", "none");
}else {
 $("[id*=idAText").css("display", "block");
}

see below images

enter image description here

9
  • Can you confirm that jQuery is correctly loaded before your JavaScript? Commented Oct 22, 2018 at 7:58
  • In the apex:inputCheckbox, onchange event handler remove {!}. Eg. onchange="toggledDisplay();" Commented Oct 22, 2018 at 8:01
  • @KeithC Yes I loaded jquery in the head tag. Commented Oct 22, 2018 at 8:07
  • @Ranga sorry it is Typo I manually wrote the code here. I will edit it. problem still exits. Commented Oct 22, 2018 at 8:08
  • Is your alert showing something? i.e Is it finding the correct dom element? Also consider putting your js script inside onload function. Eg. $( document ).ready(function() { //your js function }); Commented Oct 22, 2018 at 8:10

2 Answers 2

2

var TMid = $("[id*=checkBoxTMId]").is(':checked'); in this, I don't see "checkBoxTMId" Id assigned to any checkbox.

If you want to gray out the field then, in this case, you can set disabled to true for the input field. Please refer below snippet for the reference-

<apex:inputfield id="idAText" styleclass="inputFieldToDisable" value="{!ttlookup.a}" />
<apex:inputCheckbox id="checkBoxAId" styleClass="frstCheckbox" value="{!Acheckbox}" onchange="disableField(this)" />

<style>
    function disableField(e) {
        var fild = document.getElementsByClassName("inputFieldToDisable");
        if(e.checked) {
            fild[0].disabled = true;
            fild[0].value = '';
        } else {
            fild[0].disabled = false;
        }
    }
</style>
2
  • 1) It is Typo error I modified the post for other 2) do you know why document.getElementById(..) did not work? Commented Oct 22, 2018 at 9:32
  • We can use document.getElementById(..) although salesforce internally changes the Id for the tags. You can check Id value changed from browser console by using inspect element. Commented Oct 22, 2018 at 9:43
1

It is best practise to use Salesforce inbuilt functionality rather than using JS based solutions. This will avoid having to maintain code to support different browsers etc. We can do something like,

VF page:

<apex:panelGrid columns="5" id="theGrid" width="80%"> 
<apex:outputLabel > A </apex:outputLabel> 
<apex:inputfield id="idAText" value="{!ttlookup.a}" /> 

<apex:inputCheckbox value="{!Acheckbox}"> 
<apex:actionSupport event="onchange" action="{!toggleFields}" rerender="idRMText1,idRMText2" /> 
</apex:inputCheckbox> 

<apex:outputLabel > Check </apex:outputLabel> 

<apex:outputLabel>B </apex:outputLabel> 
<apex:inputfield id="idRMText1" value="{!ttlookup.B}" rendered="{!!fieldToggled}"/> 
<apex:outfield id="idRMText2" value="{!ttlookup.B}" rendered="{!fieldToggled}"/> 
</apex:panelGrid>

Apex Class:

public class PageController { 

public Boolean fieldToggled {get;set;} 

public PageController() { 
fieldToggled = false; 
} 

public void toggleFields() { 
fieldToggled = !fieldToggled; 
} 
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.