I have a requirement where when an option is selected in the picklist, one of the checkbox should be checked(whose id is PK1 in the code below). I tried executing using Javascript but somehow it's not working. Can someone help me? Here is the code:

                    <apex:outputLabel value="System"/>

                    <apex:outputPanel >

                             <apex:selectList id="sys" value="{!sys}"  size="1" onselect="checkthis()">

                                   <apex:selectOption itemvalue="None" itemLabel="--None--"/>

                                  <apex:selectOption itemvalue="1" itemLabel="1" />

                                  <apex:selectOption itemvalue="2" itemLabel="2"/>

                                   <apex:selectOption itemvalue="3" itemLabel="3"/>

                                    <apex:selectOption itemvalue="4" itemLabel="4"/>

                                  <apex:actionSupport event="onchange" reRender="thePageBlock"  status="status"/>

                            <apex:actionStatus startText="Showing Options" id="status" />


<apex:pageBlockSection title="Fe" columns="2" id="pb8"

                               rendered="{!syslicense != '--None--'}">
               <apex:inputField value="{!Internal__c.Bat__c}"/>

               <apex:inputField value="{!Internal__c.Imp__c}" rendered="{!IF(syslicense =='2',false,true)}"/>

               <apex:inputField value="{!Internal__c.Bat1__c}" id="Batvis"/>

               <apex:inputField value="{!Internal__c.Mot__c}" id="PK1"/>
<script type="text/javascript">
    function checkthis() {
        if (document.getElementById('{!$Component.page.form.thePageBlock.pb7.syslic}') == "2") {
            document.getElementById('{!$Component.page.form.thePageBlock.pb8.PK1}').checked = true;
  • Hi, Thanks for your reply. I tried that too and it still doesn't work – Venkat Dec 25 '14 at 23:18

The problem is that you are not accessing the value of the select element but instead comparing the the html object returned by document.getElementById with the string literal "2". Change the if condition to the below statement and it should work just fine. And also the the reason salesforce has provided the $Component is because one can directly access the vf component ID without having to reference the actual HTML id of the component.

if(document.getElementById('{!$Component.sys}').value == "2") {

Also remove the semi-colon after which you have added at the end of the if statement.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.