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

getElementById returning null using onmouseout
Hi,
I am trying to call a simple js function to default a field value in VF from another field, but the js keeps telling me that the field I am trying to get is null. I have tried various ways to make this work; the current code is below.
The message I get is: TypeError: userNameFld is null
Any ideas?
Thanks
I am trying to call a simple js function to default a field value in VF from another field, but the js keeps telling me that the field I am trying to get is null. I have tried various ways to make this work; the current code is below.
The message I get is: TypeError: userNameFld is null
</apex:form> <apex:pageBlock rendered="{!validPermissions}" mode="edit"> <apex:pageBlockSection title="Create a New User" collapsible="false" columns="1"> <apex:pageBlockSectionItem helptext="The User's email address" rendered="{!NOT(fromEditPerson)}"> <apex:outputLabel value="Email Address"/> <apex:inputField id="newEmailFld" value="{!newUser.Email}" onmouseout="defaultUsername();" style="width: 250px !important;" required="true" /> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem helptext="The the user ID that the user will log in with"> <apex:outputLabel value="User Name"/> <apex:inputField id="userNameFld" value="{!newUser.Username}" style="width: 250px !important;" required="true" /> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <script type="text/javascript"> function defaultUsername() { var userNameFld = document.getElementById("userNameFld"); var newEmailFLD = document.getElementById("newEmailFld"); if (userNameFld.value === null) { userNameFld.value = newEmailFLD.Value; } } </Script>
Any ideas?
Thanks
Here is component reference.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_component.htm
And on the javascript '=== null' is for object. You need use (userNameFld === null) or (userNameFld.value == '')
All Answers
User $Component reference
document.getElementById('{!$Component.userNameFld}')
Use chrome's developer tools to see the DOM structures.
Uncaught SyntaxError: Invalid or unexpected token
Here is component reference.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_component.htm
And on the javascript '=== null' is for object. You need use (userNameFld === null) or (userNameFld.value == '')
Although I have to say it seems to defeat the object of getElementById...