0

enter image description hereI have a VF Page where I wanted to get the record details when I check the checkbox. So I am trying to achieve this through javascript.

Here is the call the Javascript on click of 'Ok' button.

<apex:pageBlockButtons location="bottom">
            <div align="Right" draggable="false">
                    <apex:commandButton value="Ok" id="selectEmp" onClick="selectAllEmployees();">
                    </apex:commandButton>
                    <apex:commandButton value="Cancel" onclick="window.top.close()" />
            </div>
        </apex:pageBlockButtons>

function selectAllEmployees() {                     
        var selectedbox = document.getElementsByTagName('input');
        var checkboxesChecked = [];
        var selectedNames = [];
        for(var i=0; i<selectedbox.length; i++) {
            if(selectedbox[i].type == "checkbox") {
                if(selectedbox[i].checked){
                  alert('inside if'+selectedbox[i].checked); //It is retriving value as true on this alert
                  var fname = document.getElementsById('{!$Component.employeeLookupPage.EmployeeForm.employeeList.employeeBlock.tblResults.fname}').value;
                  alert(fname); //I am not able to get the value of First name here.
                   selectedNames.push(fname);
                }
            }
        }           
}
2

$Component doesn't play well when you put the values in any sort of data table, so you need to get creative.

Personally, I'd just drop the value directly on to the checkbox:

<apex:inputCheckbox value="{!row.selected}" html-data-first-name="{!row.record.FirstName}" />

[].forEach.call(document.querySelectorAll('input:checked'),
    function(v) { alert(v.dataset.firstName); });

This is the least error-prone method for what you're attempting to do, and should be supported by any browser that Salesforce supports.

  • I have no words. I have spend enough time on this and your solution greatly helped. Thanks a lot!!! – user28452 Feb 23 '17 at 8:29
  • I have one other question. If I would like to add a second attribute value I added like the following. <apex:inputCheckbox value="{!e.isSelected}" id="inputId2" html-data-full-name="{!e.employee.FullName__c}" html-data-email-value="{!e.employee.Email__c}" > . It is not returning the second value. – user28452 Feb 23 '17 at 8:37
  • @user28452 You can add as many as you like. Just prefix it with html-, and remember that - characters change the next character to uppercase in JavaScript. – sfdcfox Feb 23 '17 at 8:38
  • I guess you have responded before to my question :) With your explanation how the second attribute value will be retrived. Is it like v.dataset.Emailvalue? – user28452 Feb 23 '17 at 8:42
  • @user28452 if you use html-data-email-value, then the JavaScript will be v.dataset.emailTemplate. – sfdcfox Feb 23 '17 at 8:48

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.