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
tgganeshtgganesh 

Retrieve selected checkbox value

Hi All,

 

             I am trying to collect a value of the selected checkbox, am new to visualforce.

<apex:page controller="chooseColor">
    <apex:form id="Form1" >
    <script type="text/javascript">
        function ToggleInput(theId)
            {
                var e = document.getElementById(theId);
                alert(e.value);
            }
        function ToggleInput2(Idd)
            {
                var e = document.getElementById(theId);
                alert(e.value);
            }

    </script>
    
<apex:selectList id="chooseColor" value="{!string1}" onchange="ToggleInput('{!$Component.chooseColor}');">
    <apex:selectOption itemValue="red" itemLabel="Red"/>
    <apex:selectOption itemValue="white" itemLabel="White"/>
    <apex:selectOption itemValue="blue" itemLabel="Blue"/>            
</apex:selectList>

<apex:selectCheckboxes id="chooseColor1" value="{!string1}" onchange="ToggleInput2('{!$Component.chooseColor1}');">
    <apex:selectOption itemValue="red" itemLabel="Red" id="ID1" title="1"/>
    <apex:selectOption itemValue="white" itemLabel="White" id="ID2" title="2"/>
    <apex:selectOption itemValue="blue" itemLabel="Blue" title="3"/>            
</apex:selectCheckboxes>  

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

 

 Apex Controller Code:

public class chooseColor {
    String s = 'blue';
    public String Val{get;set;}
 
    public String getString1() {
        return s;
    }
                        
    public void setString1(String s) {
        Val = s;
        system.debug('Value+++'+Val);
        this.s = s;
        
    }
}

 When i try the apex:selectList am getting proper values, but am getting undefined for apex:selectCheckbox tag's. Please tell me where i have done the mistake.

 

else tell me how to find the selected checkbox. I have three checkbox and am selecting only two, i need to find the selected checkbox alone. give some idea.

 

Thanks in advance.. :)

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

--------------- Vf page-----------

<apex:page controller="chooseColor">

    <apex:form id="Form1" >

    <script type="text/javascript">

        function ToggleInput(theId)

            {

                var e = document.getElementById(theId);

                alert(e.value);

            }

        function ToggleInput2(Idd)

            {

             

                alert(Idd.value);

            }

 

    </script>

   

<apex:selectList id="chooseColor" value="{!string1}" onchange="ToggleInput('{!$Component.chooseColor}');">

    <apex:selectOption itemValue="red" itemLabel="Red"/>

    <apex:selectOption itemValue="white" itemLabel="White"/>

    <apex:selectOption itemValue="blue" itemLabel="Blue"/>           

</apex:selectList>

 

<apex:selectCheckboxes id="chooseColor1" value="{!string1}" onchange="ToggleInput2(this);">

    <apex:selectOption itemValue="red" itemLabel="Red" id="ID1" title="1"/>

    <apex:selectOption itemValue="white" itemLabel="White" id="ID2" title="2"/>

    <apex:selectOption itemValue="blue" itemLabel="Blue" title="3"/>            

</apex:selectCheckboxes> 

 

    </apex:form>

</apex:page>

---------------- Apex controller -----------------

public class chooseColor {

    String s = 'blue';

    public String Val{get;set;}

 

    public String getString1() {

        return s;

    }

                       

    public void setString1(String s) {

        Val = s;

        system.debug('Value+++'+Val);

        this.s = s;

       

    }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

--------------- Vf page-----------

<apex:page controller="chooseColor">

    <apex:form id="Form1" >

    <script type="text/javascript">

        function ToggleInput(theId)

            {

                var e = document.getElementById(theId);

                alert(e.value);

            }

        function ToggleInput2(Idd)

            {

             

                alert(Idd.value);

            }

 

    </script>

   

<apex:selectList id="chooseColor" value="{!string1}" onchange="ToggleInput('{!$Component.chooseColor}');">

    <apex:selectOption itemValue="red" itemLabel="Red"/>

    <apex:selectOption itemValue="white" itemLabel="White"/>

    <apex:selectOption itemValue="blue" itemLabel="Blue"/>           

</apex:selectList>

 

<apex:selectCheckboxes id="chooseColor1" value="{!string1}" onchange="ToggleInput2(this);">

    <apex:selectOption itemValue="red" itemLabel="Red" id="ID1" title="1"/>

    <apex:selectOption itemValue="white" itemLabel="White" id="ID2" title="2"/>

    <apex:selectOption itemValue="blue" itemLabel="Blue" title="3"/>            

</apex:selectCheckboxes> 

 

    </apex:form>

</apex:page>

---------------- Apex controller -----------------

public class chooseColor {

    String s = 'blue';

    public String Val{get;set;}

 

    public String getString1() {

        return s;

    }

                       

    public void setString1(String s) {

        Val = s;

        system.debug('Value+++'+Val);

        this.s = s;

       

    }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
tgganeshtgganesh

Thanks Jain... :)

 

its working fine.. :)

 

is it possible to get the number of checkbox selected directly or i need to write code and loop through it to find the value?