function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sharma_ffdcsharma_ffdc 

Issue with getting element using ElementId

I am trying below code to get the element using elementId but alert message is showing null value. Can anyone please suggest why it is behaving like this :

<apex:page standardController="Account">
  <apex:form >
      <apex:inputField value="{!Account.name}" id="accName"/>
      <apex:commandButton value="Test" onclick="testFunction(); return false;"/>
  </apex:form>
  <script>
  function testFunction(){
      alert(document.getElementById('{!$Component.accName}'));
  }
  </script>
</apex:page>
Best Answer chosen by sharma_ffdc
Satish_SFDCSatish_SFDC
In this case your inputField is nested in a form tag.
Try giving an ID to the form tag as well and access your component like:
"{!$Component.formId.accName}"


Hope this helps.

Regards,
Satish Kumar