Useful JavaScript functions for SharePoint 2013 List Forms

Useful JavaScript functions for SharePoint 2013 List Forms

The following are useful functions you can use in your SharePoint 2013 OOTB list forms, In order to use these functions you will need to:
A. Make sure you have an updated jQuery reference locally on the form page or globally for your site.
B. Add these functions locally on the form page or globally for your site.
C. Make sure to call these function with a “$( document ).ready()” function.

Here are the functions:

1. Hide a field on a form in New/Edit/Display mode by Title –
{code type=javascript}
function HideFieldOnForm(fieldTitle) {
$(‘h3:contains(“‘ + fieldTitle + ‘”)’).closest(‘tr’).hide();
}
{/code}
2. Show a field on a form in New/Edit/Display mode by Title –
{code type=javascript}
function HideFieldOnForm(fieldTitle) {
$(‘h3:contains(“‘ + fieldTitle + ‘”)’).closest(‘tr’).toggle(“slow”);
}
{/code}
3. Disable a field on a form in New/Edit mode by Title (Of input type – single line of text or multiple line of text) –
{code type=javascript}
function DisableFieldOnForm(fieldTitle) {
$(“input[title*='” + fieldTitle + “‘]”).attr(‘disabled’, ‘disabled’);
}
{/code}


4. Disable a Choice\Lookup Select Box\Drop down type field on a form in New/Edit mode by Title –
{code type=javascript}
function DisableSelectField(fieldTitle) {
$(“select[title*='” + fieldTitle + “‘]”).attr(‘disabled’, ‘disabled’);
}
{/code}
5. Set a Choice\Lookup Select Box\Drop down type field Value on a form in New/Edit mode by Title –
{code type=javascript}
function SetLookupVal(fieldTitle,val) {
$(“select[title*='”+ fieldTitle +”‘]”).val(val);
}
{/code}
6. Hide\Show field when checking\unchecking a Yes\No checkbox –
{code type=javascript}

function HideShowByCheckBox(CheckBoxTitle,FieldToShowAndHide) {
$(“input[title*='”+ CheckBoxTitle +”‘]”).change(function() {
if(this.checked) {
//Uses function number 2 above
ShowfieldDispform(‘”+ FieldToShowAndHide +”‘);
}
else {
//Uses function number 1 above
HideFieldOnForm(‘”+ FieldToShowAndHide +”‘);
}
});
}

{/code}

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *