Activity Forums Salesforce® Discussions How to disable a input Text and then enable it based on selected value of check box?

  • Shekhar Gadewar

    Member
    July 22, 2016 at 9:14 am

    Try using OnClick() + ActionFunction

  • Shubham

    Member
    July 26, 2016 at 10:23 am

    Hi Tanu,

    Try this code

    1. Using apex input checkbox and apex input text

    VF page

    <apex:page controller="DisableInputTextCntrl">
    <script>
    function myFunction(checkboxId){
    var flag = document.getElementById(checkboxId).checked;
    callAction(flag);
    }
    </script>
    <apex:form>
    <apex:actionFunction name="callAction" reRender="yourText" action="{!changeFlag}">
    <apex:param value="" name="flagValue"/>
    </apex:actionFunction>
    <apex:inputCheckbox id="yourBox" styleClass="test" onclick="myFunction('{!$Component.yourBox}')"/>
    <apex:inputText id="yourText" disabled="{!(!flag)}"/>

    </apex:form>
    </apex:page>

    Controller

    public class DisableInputTextCntrl {
    public boolean flag{get;set;}

    public DisableInputTextCntrl(){
    flag = false;
    }

    public void changeFlag(){
    flag = boolean.valueOf(ApexPages.currentPage().getParameters().get('flagValue'));
    }
    }

    2. Using HTML input checkbox and input text

    VF page

    <apex:page>

    <input type="text" id="yourText" disabled="true" />
    <input type="checkbox" id="yourBox" />
    <script>
    document.getElementById('yourBox').onchange = function() {
    document.getElementById('yourText').disabled = !this.checked;
    };
    </script>
    </apex:page>

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos