4

There are already some questions about this but i did not find a working solution.

I'm trying to access in a javascript function the selected lookup value ID of an input field.

Do you know how can i do that?

My VF component:

<apex:component Controller="FController" extensions="FCompExtension" allowDML="true">  
    <apex:attribute name="recordId" description="Record Id" type="id" assignTo="{!record_Id}" required="true"/>   
    <apex:form >
    <script>
        function ShowChinaOfficeSelection(){
            //First Way, doesn t work
            var SalesOrgId= '{!NewAccountForLFC.Sales_Org__c}'; 
            //Second Way, this gives me the text value
            var SalesOrgId=document.getElementById('{!$Component.lookupfield}').value;  
            console.log('SelectedSalesOrdId '+SalesOrgId);      

         }
    </script>

  <!-- LookUp Field -->
  <apex:inputfield value="{!NewAccountForLFC.Sales_Org__c}" onchange="ShowChinaOfficeSelection()" id="lookupfield"> 
  </apex:inputField>     


 </apex:form>     
</apex:component>
0

There is a hidden field rendered in the page which corresponds to the lookup field. This hidden field contains the ID of the lookup value after the selection is made in the lookup dialog.

It is named the same as the lookup field's (visible) textbox and is suffixed with _lkid.

var SalesOrgText = document.getElementById('{!$Component.lookupfield}').value;
var SalesOrgId = document.getElementById('{!$Component.lookupfield}_lkid').value;

console.log('SelectedSalesText: ' + SalesOrgText);
console.log('SelectedSalesOrgId: ' + SalesOrgId);

Given a page like this:

<apex:page standardcontroller="Contact" id="thePage">
    <apex:form id="theForm">
        <apex:inputField id="theInput" value="{!Contact.AccountId}"/>
    </apex:form>
</apex:page>

An input:text field will be rendered with the id: thePage:theForm:theInput which will contain the name of the record which was selected.

As well as an input:hidden field with the id: thePage:theForm:theInput_lkid which will store the selected record's id value.

  • Fantastic explanation Mark! – Enry Mar 31 '15 at 19:32
  • What is the !$Component here? the VF page or VF component? – Hasantha Jan 15 '16 at 1:54
  • It is a DOM element. developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – Mark Pond Jan 15 '16 at 2:00
  • can we access this through jQuery? – Hasantha Jan 15 '16 at 2:30
  • Create a question of your own regarding accessing a DOM element using this syntax via jQuery and any problems you're having doing so. Comments are not the place for a conversation about an altogether different question. -- nevermind, I see that you did create a question. – Mark Pond Jan 15 '16 at 4:35

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.