Automatically fill in fields via JQuery in SharePoint

Last time we talked about hiding fields in the editform, newform and dispform. But what if we don’t want to hide a certain field, but rather get some value and put it in? An example could be to fill in a unique number or the day of the week.

Code
First, add the reference to the jquery file on your page. Next, add the code:

<script>
$(document).ready(function() { 
  var fieldToAddText = $(":input[title='Your Fieldname']");
  if( fieldToAddText .val() != undefined ) {
    fieldToAddText .val("add some text");
  }; 
};
</script>

What this code does, is simply: when the page is loaded, get the field where the name of the field is a certain value. If the field exists, write something in the textfield.

About: Marijn

Marijn Somers (MVP) has over 14 years experience in the SharePoint world, starting out with SP2007. Over the years the focus has grown to Office 365, with a focus on collaboration and document management. He is a business consultant at Balestra and Principal Content Provider for "Mijn 365 Coach" that offers dutch employee video training. His main work tracks are around user adoption, training and coaching and governance. He is also not afraid to dig deeper in the technicalities with PowerShell, adaptive cards or custom formatting in lists and libraries. You can listen to him on the biweekly "Office 365 Distilled" podcast.