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>
Best Answer
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
.Given a page like this:
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.