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 –

1
2
3
function HideFieldOnForm(fieldTitle) {
   $('h3:contains("' + fieldTitle + '")').closest('tr').hide(); 
}

2. Show a field on a form in New/Edit/Display mode by Title –

1
2
3
function HideFieldOnForm(fieldTitle) {
   $('h3:contains("' + fieldTitle + '")').closest('tr').toggle("slow"); 
}

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) –

1
2
3
function DisableFieldOnForm(fieldTitle) {
    $("input[title*='" + fieldTitle + "']").attr('disabled', 'disabled');
}



4. Disable a Choice\Lookup Select Box\Drop down type field on a form in New/Edit mode by Title –

1
2
3
function DisableSelectField(fieldTitle) {
    $("select[title*='" + fieldTitle + "']").attr('disabled', 'disabled');
}

5. Set a Choice\Lookup Select Box\Drop down type field Value on a form in New/Edit mode by Title –

1
2
3
function SetLookupVal(fieldTitle,val) {
   $("select[title*='"+ fieldTitle +"']").val(val);
}

6. Hide\Show field when checking\unchecking a Yes\No checkbox –

1
2
3
4
5
6
7
8
9
10
11
12
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 +"');               
      }
   });
}

Easy Tabs v-5 for SharePoint 2013

Easy Tabs v-5 for SharePoint 2013

Easy Tabs v-5 is a great script you can use to bind your SharePoint web parts in Tabs that’s been around since MOSS 2007!
The best thing about this tabs script is that it’s generic and that it works in a “Copy & Paste” kinda way, so that it can be used simply by any end-user without knowing JavaScript.
It binds all the web parts inside the same web part zone in tabs and uses their titles for their related tab title.
You can generate the script here at the User Managed Solutions site
But they haven’t released a 2013 version.. Luckily Venkatesh Ramasamy fixed the code so that it would work in SharePoint 2013.
His code along with instructions for using the script can be found here.
I got to use this script lately and it worked perfectly, I’ve made some CSS adjustments and added some CSS3 simple transitions to make it look more modern as you can see here:

Easy Tabs 2013

Easy Tabs 2013




Also I’ve added some documentation to the script, to make changes easier, you can copy my modified script from here –

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<style type="text/css">

/* General tabs style active and in-active  */
.et-tab { padding:3px 10px; display:inline-block; cursor:pointer;}

/* General inner text tabs style active and in-active  */
.et-tab .ms-webpart-titleText { color:white; font:24px arial; padding: 10px 20px;  }

/* active tabs style  */
.et-activetab {background-color:#009fdd;  border:solid 1px #009fdd; padding-top:12px; }

/* inactive tabs style including CSS3 transition on mouse hover */
.et-inactivetab { background-color:#0089c2; padding-top:2px; font-family:Arial; border:solid 1px #0089c2; 
 transition:0.2s ease-out; -webkit-transition: all 0.2s ease-out; -moz-transition: all 0.2s ease-out; -o-transition: all 0.2s ease-out;
 }
.et-inactivetab:hover {
    padding-top:12px; background-color:#009fdd;
}

/* speration line style  */
.et-separator{height:5px;background-color:#0089c2;}
.et-tabrow{white-space:nowrap;}
.et-offscreen{position:absolute;max-height:1px;max-width:1px;top:-9999px;}

</style>

<script type="text/javascript">
/* Easy Tabs v 5.0 * Copyright (c) 2009-2010 Christophe Humbert * http://www.pathtosharepoint.com */
/* Updated for SharePoint 2013 by Venkatesh R. */
/* Slightly modified by Guy P.  */

function isEditMode()
{
    var boolPageInEditMode = false;
    
    // Check page is in Edit/Design Mode or not.
    if(document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode != null)
    {
        var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
    
        if (inDesignMode == "1")
        {
            boolPageInEditMode = true;
        }
    }
    
    if(document.forms[MSOWebPartPageFormName]._wikiPageMode != null)
    {
        var wikiInEditMode = document.forms[MSOWebPartPageFormName]._wikiPageMode.value;
        if (wikiInEditMode == "Edit")
        {
            boolPageInEditMode = true;
        }
    }

    return boolPageInEditMode;
}

(function () {
    var AP = "",
        sec = 0,
        Header = "none",
        Split = "No",
        /* If you want to add an Expand all tab give the following var a title */
        Expand = "",
        Print = "";
    if (isEditMode() == true) {
        return;
    }
    var el = document.getElementsByTagName("SCRIPT"),
        p = el[el.length - 1],
        sT, a, sep, tabRow;
    do {
        p = p.parentNode;
        sT = p.innerHTML.split("MSOZoneCell_WebPart");
    } while (sT.length < 4 && p.parentNode.id != "DeltaPlaceHolderMain")
    if (p.getAttribute("contenteditable") == "true") {
        return;
    }
    if (p.nodeName == "DIV") {
        sep = document.createElement("div");
        p.insertBefore(sep, p.firstChild);
        tabRow = document.createElement("div");
        p.insertBefore(tabRow, p.firstChild);
    } else {
        sep = document.createElement("td");
        var sepTR = document.createElement("tr");
        sepTR.appendChild(sep);
        tabRow = document.createElement("td");
        var tabTR = document.createElement("tr");
        tabTR.appendChild(tabRow);
        if (p.nodeName == "TBODY") {
            p.insertBefore(sepTR, p.firstChild);
            p.insertBefore(tabTR, p.firstChild);
        } else if (p.nodeName == "TR") {
            p.parentNode.insertBefore(tabTR, p);
            p.parentNode.insertBefore(sepTR, p);
        } else {
            return;
        }
    }
    sep.className = "et-separator";
    tabRow.className = "et-tabrow";
    var children = p.childNodes;
    p = p.parentNode;
    var etRoot = [],
        etHeader = [],
        etTab = [],
        tabsID = [];
    for (var j = 0; j < children.length; j++) {
        try {
            var d = children[j].getElementsByTagName("div");
            for (i = 0; i < d.length; i++) {
                if (d[i].id.indexOf("_ChromeTitle") > 1) {
                    var WPid = d[i].id.replace(/_ChromeTitle/, "");
                    WPid = WPid.replace(/WebPart/, "");
                    if (d[i].innerHTML.indexOf("(Hidden)") == -1) {
                        var up = d[i];
                        while (up != children[j]) {
                            if (up.parentNode.innerHTML.indexOf('id=WebPart' + WPid + ' ') >= 0 || up.parentNode.innerHTML.indexOf('id="WebPart' + WPid + '" ') >= 0) {
                                WPid = "et" + WPid;
                                etHeader[WPid] = up;
                                etRoot[WPid] = children[j];
                                
                                var spanWebPartTitle = document.createElement("span");
                                spanWebPartTitle.innerHTML = '<span style="text-align:justify;" class="ms-webpart-titleText">' + getText(d[i].getElementsByTagName("span")[0]) + '</span>';                                
                                etTab[WPid] = spanWebPartTitle;
                                etTab[WPid].id = WPid;
                                etTab[WPid].className = "et-tab et-inactivetab";
                                etTab[WPid].onclick = function () {
                                    activateTab(this);
                                };
                                tabRow.appendChild(etTab[WPid]);
                                tabsID.push(WPid);
                                break;
                            }
                            up = up.parentNode;
                        }
                    }
                }
            }
        } catch (e) {}
    }
    var Tabs = tabRow.getElementsByTagName("span"),
        TabCount = Tabs.length;
    if (Split == "Yes") {
        var sd = document.createElement("div"),
            index = Math.floor(TabCount * 0.5);
        tabRow.insertBefore(sd, Tabs[index]);
    }
    if (AP.length && sec > 0) {
        sec = sec * 1000;
        interval = "";
        a = document.createElement("span");
        a.innerHTML = "|>";
        a.className = "et-tab et-inactivetab";
        a.onclick = function () {
            if (interval == "") {
                this.innerHTML = "||";
                interval = window.setInterval(function () {
                    Autoplay();
                }, sec)
            } else {
                this.innerHTML = "|>";
                window.clearInterval(interval);
                interval = ""
            }
        };
        tabRow.appendChild(a);
        var Autoplay = function () {
            for (i = 0; i < TabCount; i++) if (Tabs[i].className == "et-tab et-activetab") {
                var j = (i + 1) % TabCount;
                activateTab(Tabs[j]);
                break
            }
        };
        if (AP == "Play") {
            a.innerHTML = "||";
            interval = window.setInterval(function () {
                Autoplay();
            }, sec)
        };
    }
    if (Expand.length) {
        a = document.createElement("span");
        a.innerHTML = '<span style="text-align:justify;" class="ms-webpart-titleText">' + Expand + '</span>';
        a.className = "et-tab et-inactivetab";
        a.onclick = function () {
            for (i = 0; i < tabsID.length; i++) {
                etTab[tabsID[i]].className = "et-tab et-inactivetab";
                etRoot[tabsID[i]].className = etRoot[tabsID[i]].className.replace(/et-offscreen/g, "");
                etRoot[tabsID[i]].style.pageBreakAfter = "always";
                etHeader[tabsID[i]].style.display = "";
            }
        };
        tabRow.appendChild(a);
    }
    if (Print.length) {
        a = document.createElement("span");
        a.innerHTML = Print;
        a.className = "et-tab et-inactivetab";
        a.onclick = function () {
            this.style.display = "none";
            var f = document.getElementById("s4-workspace") || document.getElementsByTagName("body")[0],
                ed = p.parentNode.insertBefore(document.createElement(p.nodeName), p);
            f.appendChild(p);
            for (j = 0; j < f.childNodes.length - 1; j++) {
                try {
                    f.childNodes[j].className += " et-offscreen";
                } catch (e) {}
            }
            a = document.createElement("span");
            a.innerHTML = "Back to Page";
            a.className = "et-tab et-inactivetab";
            a.onclick = function () {
                this.previousSibling.style.display = "inline-block";
                this.parentNode.removeChild(this);
                ed.parentNode.insertBefore(p, ed);
                ed.parentNode.removeChild(ed);
                for (j = 0; j < f.childNodes.length; j++) {
                    try {
                        f.childNodes[j].className = f.childNodes[j].className.replace(/\s*et-offscreen/g, "");
                    } catch (e) {}
                }
            };
            tabRow.appendChild(a);
        };
        tabRow.appendChild(a);
    }

    function activateTab(t) {
        if(t != null)
        {
            document.cookie = tabsID.join("_") + "=" + t.id + ";path=/";
            for (i = 0; i < tabsID.length; i++) {
                etHeader[tabsID[i]].style.display = Header;
                if (t.id == tabsID[i]) {
                    etTab[tabsID[i]].className = "et-tab et-activetab";
                    etRoot[tabsID[i]].className = etRoot[tabsID[i]].className.replace(/\s*et-offscreen/g, "");
                } else {
                    etTab[tabsID[i]].className = "et-tab et-inactivetab";
                    etRoot[tabsID[i]].className += " et-offscreen";
                }
            }
        }
    }
    var m = GetCookie(tabsID.join("_")) ? GetCookie(tabsID.join("_")) : tabsID[0];
    activateTab(etTab[m]);
})();


//Get InnerText or TextContent
function getText(objElement)
{
    var strText = "";
    if(objElement != null)
    {
        if(document.all)
        {
            strText = objElement.innerText;
        } 
        else
        {
            strText = objElement.textContent;
        }
    }
    
    return strText;
}

</script>