Thursday, May 6, 2010

Changing a Form Field Label

A question was posted in the CRM Forums on how to change a form field's label using javascript onload.  One thing people might not consider is that if you just change the innerText of the field label and the field is required then you will override the asterisk image that is next to the label.

To change the field label correctly, we will need to find the label element and change it's text to our new label rather than overriding the innerText.  Below is an image of the DOM of a field label in Dev Toolbar where I'm highlighting the text that we are going to find and change.

Here's the script to do so:

function changeFieldLabel(fieldName, newLabel)
     var field = crmForm.all[fieldName + "_c"];
     if (field != null)
          crmForm.all[fieldName + "_c"].firstChild.firstChild.nodeValue = newLabel;

Which you can use like so:
changeFieldLabel("name", "New Name");

No comments:

Post a Comment