Why getting error can not read property 'value' of null?

335    Asked by AndreaBailey in Salesforce , Asked on Jul 19, 2021

I am trying to get the value of a text field in a VF page in JavaScript. Below is my VF page and getting the error “cannot read property of null”.

 [removed] function initialize(){ alert('called' + document.getElementById("thePage:theform:atextId").value); } [removed] 
When I click the link I get this error in the inspect window. Cannot read property 'value' of null I might be missing something here. I checked the path of the text field using inspect window and I saw: <input id="thepage:theform:atextId" maxlength="8" name="thepage:theform:atextId" size="20" type="text" value="731645"> What am I missing here. How to resolve this,why do I get a null?
Answered by anu rhea

The reason for getting the error “cannot read property of null” is typo mistake. In your case, it was a simple typo error: the ID value is cAsE-sEnSiTiVe. alert('called' + document.getElementById("thepage:theform:atextId").value); // ^ This P was capitalized Always make sure you're matching the case for your Id values. As a more practical solution, consider using $Component: alert('called' + document.getElementById("{!$Component.thePage.theform.atextId}").value); You can simply resolve this error by using $Component, $Component will resolve the element Id correctly (in a case-insensitive manner) so you don't have to worry about it as much.



Your Answer

Interviews

Parent Categories