How can i Modify the Label of Form Fields With Client Scripts

nysbigdave
Kilo Expert

Hey Guys I am attempting to rewrite the label if a user has itil role..
however I believe the syntax maybe off.. can you help?


the field name is called u_comments

 

 

function onLoad() {
  if (!g_user.hasRole("itil"))
return;

var field = 'u_comments';
var fieldFormName = g_form.getTableName() + "." + field;
var fieldLabelName = "label." + fieldFormName;
var ctrl = $(fieldLabelName);
  if (!ctrl)
  return;
  var label = ctrl.select('label')[0];
  if (!label)
  return;
  var s = label.innerHTML;
  label.innerHTML = s.substring(0, s.length — 1) + ' (' + getMessage('Customer visible') + '):';
  //label.setStyle({fontWeight:"bold"});
}

1 ACCEPTED SOLUTION

nysbigdave
Kilo Expert

Mark Stanger (Crossfuze) Resolved this for me



function onLoad() {
      if(!g_user.hasRole('itil'))
              return;



      //Change the description label to 'My New Label' with bold red text
      changeFieldLabel('u_comments', 'My New Label', 'red', 'bold');
}



function changeFieldLabel(field, label, color, weight){
      try{
            var labelElement = $('label.' +   g_form.getControl(field).id);
            labelElement.select('label').each(function(elmt) {
                  elmt.innerHTML = label + ':';
            });
            if(color)
                  labelElement.style.color = color;
            if(weight)
            labelElement.style.fontWeight = weight;
      }catch(e){};
}


View solution in original post

11 REPLIES 11

Changing the color of the field label is not supported. The article above was written three years ago for an entirely different UI.



I like the idea of styling the labels based on conditions (much like a UI policy). It might make a good feature for a future release.



I invite you to open an enhancement request! Our product managers DO listen.


Enhancement requests: Tell us how you would improve the ServiceNow product


Hey ctomasi



I am asking about my specific question its related to this thread that's why posted it here.


Please provide me   solution.



Thanks


Aastha


Aastha,



I don't have a solution at this point. The platform does not support styling of field labels. I caution against using any solutions proposed here as create risk of upgrade problems in the future.


ohh ok I was able to change color with help of below code :


function onLoad() {


    //Type appropriate comment here, and begin script below




var system = document.getElementById('label.incident.u_choice_2');


var modules = document.getElementById('label.incident.u_choice_1');


var incidentcategory = document.getElementById('label.incident.category');


var severity = document.getElementById('label.incident.u_severity');


var shortdescription = document.getElementById('label.incident.short_description');


var description = document.getElementById('label.incident.u_html_1');


var mail = document.getElementById('label.incident.u_string_1');


var requestedduedate = document.getElementById('label.incident.u_glide_date_1');


system.style.color = "red";


modules.style.color = "red";


incidentcategory.style.color = "red";


severity.style.color = "red";


shortdescription.style.color = "red";


description.style.color = "red";


mail.style.color = "red";


requestedduedate.style.color = "red";


}



But I dont know why its changing red color to black when I change   field's choice value.



Regards


Aastha


I don't know why it is changing either. As mentioned above, accessing the DOM is considered a bad practice and almost always causes issues later.



Client Script Best Practices - ServiceNow Wiki