Support for HTML 5 placeholder attribute

Jim Coyne
Kilo Patron

I've created a function to add support for the HTML placeholder attribute. You can use it to include some extra helpful text in input fields:

find_real_file.png

It's real simple to use - just include a call to the function in an onLoad script with the name of the field/variable and the text you want displayed:



u_addPlaceholderAttribute('short_description', 'Enter a short description of the problem');

I've tested it in Chrome, Firefox and Opera on Windows 7 and it works. Of course it does not work in current version of IE, but it does work in the upcoming IE 10.

Here's the script:


function u_addPlaceholderAttribute(variableName, hint) {
  try{
      var fieldName = g_form.getControl(variableName).name.toString();
      if (Prototype.Browser.IE) {
          fieldName.placeholder = hint;
      } else {
          $(fieldName).writeAttribute('placeholder', hint);
      }
  } catch(err) {}
}

I've also attached it as an XML export of the function - just remove the ".txt" extension and import it into your instance.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Just setting this answer as correct as the actual thread was not meant as a question and it cannot be changed because it came from the original Community site.


View solution in original post

44 REPLIES 44

The script should be a global UI Script and not a Script Include.


vahini
Giga Guru

Hey Jim ,



i got a chance to use this script for one of my requirement . I tried it on catalog items it worked , i tried it on incident form it worked .



i observed that it works only for single line text fields and multi line texts fields . so any possibility to make it work for reference fields too ??


will this work on reference fields ??


It actually does as is.   Here's a screenshot of an Incident form:


find_real_file.png



And on a Catalog Item:


find_real_file.png



The trick is to use the full name of the input control for the reference field, like so - "sys_display.table_name.field_name" (e.g. sys_display.incident.caller_id).



For Catalog Variables, it's a little different - "sys_display.IO:sys_id_of_the_variable" (e.g. sys_display.IO:2a3947a10a0a020e007f12648f8790c0).



You can use your browsers "Inspect" or "Inspect element" function to get the proper names.


Hi Jim,

I need to have placeholder text foe a field of type 'HTML'. Is there a way to do this?

Sadly, I don't think this is possible for non string fields. I tried for Reference fields and HTML fields without success. We need to continue to convince ServiceNow to add HTML placeholder as a platform feature.


Best regards from Switzerland
Shiva :¬,

If this reply assisted you, please consider marking it 👍Helpful.
This enables other customers to learn from this thread.