You need to sign in to do that
Don't have an account?

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>
<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>
Try giving an ID to the form tag as well and access your component like:
"{!$Component.formId.accName}"
Hope this helps.
Regards,
Satish Kumar