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
aveforstudy1.3196212890020557Eaveforstudy1.3196212890020557E 

How to disable a input Text and then enable it based on selected value of check box

Hi,

I am in need of an urgent help. I want to disable a input text based on the selcted value from a drop down.

I am able to achieve it by using actionSupport on the change event of the dropdown, but it does not work when i want to make the disabled field back to enabled. Surprisingly the actionSupport's action does not get called.

Any help would be appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
Abhay AroraAbhay Arora

Below is the working solution for this

 

<apex:page id="thepage" standardController="Account" >
<apex:form >
    <apex:inputField value="{!Account.check__c}">
        <apex:actionSupport event="onchange" rerender="pan" status="counterStatus" />
    </apex:inputField>
            <apex:outputPanel id="pan">
            <apex:inputText id="test" disabled="{!IF(Account.check__c==true,true,false)}" />
        </apex:outputPanel>
        <apex:actionStatus id="counterStatus" startText=" (incrementing...)" stopText=" (done)"/>
</apex:form>
</apex:page>

 

 

Hi please mark it as solved if it works for you

All Answers

Chamil MadusankaChamil Madusanka

Try this

 

<apex:page controller="TestPage10Controller">  
    <apex:form >
        <apex:inputText id="iText" disabled="{!check}"/>
        
        <apex:commandButton value="Enable Input Text" action="{!methodOne}" reRender="iText"/>
    </apex:form>
  </apex:page>

 

public with sharing class TestPage10Controller 
{
    public Boolean check{get;set;}
    
    public TestPage10Controller ()
    {
        check = true;
    }
    
    public void methodOne()
    {
        check = false;
    }

}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

aveforstudy1.3196212890020557Eaveforstudy1.3196212890020557E

Hey,

Thanks for the quick response.

Actually i want to make use of <apex:actionSupport> as i want to disable the input text once the dropdown's value changes.

Here's the code snippet.

 

<apex:selectList size="1" value="{!selecteddate}">

<apex:selectOptions value="{!year}"></apex:selectOptions>
                    <apex:actionSupport event="onchange" action="{!disablefield}" reRender="jan"/>

</apex:selectList>

/*Inside the page block table i heva the following column*/

 <apex:column headerValue="Jan" >
                        <apex:inputText value="{!MB.Jan}" disabled="{!disableJan}" id="jan" />
                    </apex:column>

 

The problem is that disablefield is not called at all if the previously jan was disabled.

 

 

Chamil MadusankaChamil Madusanka

Can you post your apex method ?

Abhay AroraAbhay Arora

Below is the code you need its is for a checkbox you can just change the field

 

1.)Select box to checkbox

2.)checkbox to inputText

 

 

public with sharing class testComponent {

    public testComponent() {

    }

    public Account acc{get;set;}
    public testComponent(ApexPages.StandardController controller) {
        //acc=(account)controller.getrecord();
//        string accid=(account)controller.getrecord().id;
        acc=[select id,AccountSource,check__c from account where id='0019000000CJ6QZ'];
    }
    public pagereference changeCheckBox(){
        acc.check__c=true;
        return null;
    }

}

 

 

<apex:page id="thepage" standardController="Account" extensions="testComponent">
<apex:form >
    <apex:inputField value="{!acc.AccountSource}">
        <apex:actionSupport event="onchange" action="{!changeCheckBox}" rerender="pan" status="counterStatus" />
    </apex:inputField>
            <apex:outputPanel id="pan">
            <apex:inputField id="test" value="{!acc.check__c}"/>
        </apex:outputPanel>
        <apex:actionStatus id="counterStatus" startText=" (incrementing...)" stopText=" (done)"/>
</apex:form>
</apex:page>
                   
aveforstudy1.3196212890020557Eaveforstudy1.3196212890020557E

public void disablefield(){
        //monthsToDisable;
        isDisabled = false;
        monthsToDisable = new List<String>();
        System.debug('inside disablefield  '+monthsToDisable);
        List<Integer> tempMonth = new List<Integer>();
        Date todaysDate = System.today();
        DateTime tempDate = System.now();
        Integer Month = todaysDate.month();
        String Year = tempDate.format('yyyy');  
        System.debug('selecteddate is '+selecteddate+'current Year is '+Year);
        if(selecteddate==Year){
        if(Month!=1){
                for(integer i=Month-1;i>0;i--){
                        tempMonth.add(i);
                }
        }
        for(Integer m:tempMonth){
                if(m==1){
                        monthsToDisable.add('Jan');
                }
                if(m==2){
                        monthsToDisable.add('Feb');
                }
                if(m==3){
                        monthsToDisable.add('Mar');
                }
                if(m==4){
                        monthsToDisable.add('Apr');
                }
                if(m==5){
                        monthsToDisable.add('May');
                }
                if(m==6){
                        monthsToDisable.add('Jun');
                }
                if(m==7){
                        monthsToDisable.add('Jul');
                }
                if(m==8){
                        monthsToDisable.add('Aug');
                }
                if(m==9){
                        monthsToDisable.add('Sept');
                }
                if(m==10){
                        monthsToDisable.add('Oct');
                }
                if(m==11){
                        monthsToDisable.add('Nov');
                }
                if(m==12){
                        monthsToDisable.add('Dec');
                }
        }
        }
        System.debug('monthsToDisable  '+monthsToDisable);
    }

This function prepares a list, having the month names that needs to be disabled.

 

Following is the function that will return true or false to the disabled attribute of input text.

 

public boolean getdisableJan(){
        System.debug('monthsToDisable @@@@@'+monthsToDisable);
        boolean isDisabled;
        for(String s:monthsToDisable){
                if(s=='Jan'){
                isDisabled= true;
                return isDisabled;
            }
        }
        return isDisabled;
    }

 

aveforstudy1.3196212890020557Eaveforstudy1.3196212890020557E

please ignore

isDisabled = false;  in disablefield() function

Abhay AroraAbhay Arora

You dont have to use the FLAG in controller you can just use the checkbox as a flag and rerender the inputtext panel as given in my example

 

rendered="{!IF(acc.check__c==true,true,false)}"

 

here acc.check__c is a checkbox which when checked says true otherwise false

 

 

aveforstudy1.3196212890020557Eaveforstudy1.3196212890020557E

Hey,

If the field is enabled then its gets disabled as per the logic, but if the filed is disabled then in that case it does not go back to being enabled.

Am i missing anything, or is there any other approach i can take. I have already tried using javascript but its of no use.

aveforstudy1.3196212890020557Eaveforstudy1.3196212890020557E

thanks Abhay, but i am not making use of any fields here, its just a input text.

Abhay AroraAbhay Arora

Below is the working solution for this

 

<apex:page id="thepage" standardController="Account" >
<apex:form >
    <apex:inputField value="{!Account.check__c}">
        <apex:actionSupport event="onchange" rerender="pan" status="counterStatus" />
    </apex:inputField>
            <apex:outputPanel id="pan">
            <apex:inputText id="test" disabled="{!IF(Account.check__c==true,true,false)}" />
        </apex:outputPanel>
        <apex:actionStatus id="counterStatus" startText=" (incrementing...)" stopText=" (done)"/>
</apex:form>
</apex:page>

 

 

Hi please mark it as solved if it works for you

This was selected as the best answer
Abhay AroraAbhay Arora

Hi Please mark this as solved if your issues have been resolved

PrasanthsagarPrasanthsagar

Hi,

 

You can use a javascript in the page... if u r comfortable with it...

 

It would be very easy to implement.... 

 

 

Thanks,

Prasanth

PrasadVRPrasadVR

Hi Abhay

 

      its working good but its taking more than 30 sec of time can't we make it quick.