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

Sorry, but there does not seem to be an easy answer for that because of the use of the TinyMCE editor for HTML fields.   You could probably work out some crazy client script to do it, but it is probably not worth it.   This might be a workaround though:


Test_Catalog_Item___ServiceNow.png


You can use the g_form.showFieldMsg() method to add an informational message at the bottom of the variable - GlideForm (g form) - ServiceNow Wiki.   Not ideal, but it is something.   Just use the variable name for the "input" parameter.



Another workaround would be to use the Help tag/text on the variable.


find_real_file.png



I've got a client script that will show the Help text and then even hide the "More information" link to pretty it up.   I took a quick look on the Community but did not find it.   I could look again if you are interested.


jeremyeperdue
Giga Expert

This is great! Thank you


You are welcome.   I don't understand why it is still not supported OOB.    


veena_kvkk88
Mega Guru

Hi Jim!



This helped me greatly! But, is there a way to have multiple lines in the placeholder text? I have a string field of length 1000, and I want to have a placeholder with multiple lines like this:



[Logo]


[Job Name]


[Task Name]


[Number Range]


[Delivery Date]



The new line character isn't working.


The placeholder spec says "The placeholder text must not contain carriage returns or line-feeds."https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input   If you really need something like that, you could use a Form Annotation.