I have a apex:inputField where I'm getting lookup field. Now I want to get the Id of that selected record in lookup field in my javascript.

I'm able to get the record name but not record id

VF Page:

 <apex:inputField id="accountName" value="{!Oppty.AccountId}" onchange="accountSelected(this.value);" />

function accountSelected(data){
        console.log(' >>> Account changed... '+JSON.stringify(data));
  • did the answer solved your problem ?
    – Aman Patil
    Commented Feb 16, 2023 at 15:31

1 Answer 1


To get the Id of the selected record in your JavaScript function, you can try modifying the accountSelected function to below.

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

function accountSelected(data) {
    var selectedAccountId =document.getElementById('{!$Component.accountName}_lkid').value;
    console.log(' >>> Account Id changed... ' + selectedAccountId);

you can check this similar Thread

  • I'm getting below error: Uncaught TypeError: Cannot read properties of null (reading 'value') at accountSelected Commented Feb 17, 2023 at 9:26
  • did you tried as given above ?
    – Aman Patil
    Commented Feb 17, 2023 at 9:28
  • yes I did as given above Commented Feb 17, 2023 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .