[SalesForce] How to extract the value attribute of into a variable

I have a list of Property_Rate__c records in an Apex:repeat function. For every record in the list, a checkbox is created. The value attribute of each checkbox is the ID of the Property_Rate__c record. I am wondering: When one of the checkboxes is checked, how to extract the ID value into a variable for later use?

<apex:repeat value="{!property_rate_list}" var="propertyRate" id="propertyRateList">

    <input type="checkbox" value="{!propertyRate.Id}" class="dateCheckbox" onchange="verifyChecked($(this));" />

</apex:repeat>

verfiyChecked Function

$ = jQuery.noConflict()   
    function verifyChecked(checkbox)
    {
        $('.dateCheckbox:checked').prop('checked', false); // clear all checkboxes
        $(checkbox).prop('checked', !$(checkbox).prop('checked')); // toggle
        //changeDate($(checkbox).val());
    }

Best Answer

We are going to use actionfucntion to pass value from visaulforce to controller.

I have defined inline comments. I hope it will help you to understand the code.

HTML update

<input type="checkbox" value="{!propertyRate.Id}" 
class="dateCheckbox" onchange="return verifyChecked($(this),'{!propertyRate.Id}');" />

Javascript update

function verifyChecked(checkbox,recordid)
{

    $('.dateCheckbox:checked').prop('checked', false); // clear all property 
     checkboxes
    $(checkbox).prop('checked', !$(checkbox).prop('checked')); // toggle
    //changeDate($(checkbox).val());
    //call action function.
    callmethod(recordid);
    return false;
}

Add following code before apex:form closing tag

 <!-- This code will call actionfunction and pass the value to controller -->
 <apex:actionfunction name="callmethod" action="{!setProperty}" status="updatestatus">
     <apex:param name="propertyid" value=""/>
</apex:actionfunction>
<apex:actionstatus id="updatestatus" starttext="updating.." stoptext=""/>

Controller method

public void setProperty()
{
     //read propertyid value from page.
     String propertyid = ApexPages.currentPage().getParameters().get('propertyid');

     //user this propertyId for your logic.
}
Related Topic