0

I have a checkbox in my visualforce page. If the checkbox is checked, I want text field to appear which should be mandatory to fill. If the checkbox is unchecked then it should disappear. By default, the checkbox should be unchecked.

1) How to create a checkbox in visual force page? Everywhere I see this

<apex:inputCheckbox value="{!a.Send__c}"/>

which I believe Send__c will be a custom field on custom object. I don't have object here. I have a controller class and vf page.

4
  • You can use javascript to do this. Make field hide initially and if checkbox is checked then make it visible.
    – MIX DML
    Feb 4, 2015 at 5:26
  • @Prabhat thanks for the reply but how to create a checkbox in vf page ? If I write this <apex:inputCheckbox value="{!someXYZ}"/> I am able to see checkbox in my page but I can't name it.
    – LazyBones
    Feb 4, 2015 at 5:30
  • Name it means ..... you want to give a label to checkbox ?
    – MIX DML
    Feb 4, 2015 at 5:35
  • yes. If i put label attribute, the name does not show up.
    – LazyBones
    Feb 4, 2015 at 6:06

1 Answer 1

1

Try Below,

<script>
function inputDisplay(myCheck,inputId){
if(myCheck.checked){
    document.getElementById(inputId).style.display = 'block';
}
else {
    document.getElementById(inputId).style.display = 'none';
}
}
</script>
<apex:pageBlock>
<apex:pageBlockSection>

<apex:pageBlockSectionItem>
<apex:outputLabel value="My InputCheckBox"/>
<apex:actionRegion>
    <apex:inputCheckbox value="{!someXYZ}" onchange="inputDisplay(this,'{!$Component.myInputId}')"/>
</apex:actionRegion>
</apex:pageBlockSectionItem>

<apex:inputText id="myInputId" value="{!someString}" style="display:none;" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
2
  • thank u for the reply. This helps. But this is partially right. When I first load the page I don't want to see the text box which I can see right now. I want text box to appear only after checkbox is checked. Also, the text box has become mandatory field even if the checkbox is not checked. Thank u very much.
    – LazyBones
    Feb 4, 2015 at 14:58
  • 1
    @sshah...i have updated the code.let me know if this works
    – snehakem
    Feb 5, 2015 at 7:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .