Category Archives: JQuery

Hide certain Lookup Values from appearing on New or Edit forms

I’ve recently had an issue where there are over 2,000 items in a lookup and trying to search for the one you want was a huge pain, the code below can find certain text in the lookup options and exclude these, doing so reduces the amount of values you see and more user friendly for the end users.

From the script below, ‘Electronic Device’ was my column name, which also was a required field. The term I was looking for was “OLD” (as old devices were no longer required and therefore flagged old).

Remember to download the JQuery CDN.

<script type=”text/javascript” src=”https://TENANT.sharepoint.com/sites/YourSharePointSiteCol/SiteAssets/CDN/jquery-3.3.1.min.js”></script>
<script>

$(document).ready(function(){
$(“select[Title=’Electronic Device Required Field’]”).find(‘option:contains(“OLD”)’).remove();
});

</script>

 

Prevent selection of Lookup values on save

When you have a Lookup column you may find some results you don’t want your end users to select, for instance in my example below, the column name is ‘Team Name’ which is also a required field. This column basically shows every single team.

What the script below does is prevents the user from saving the form if “Human Resources” or “–Select a Team–” have been chosen. It also provides text under the specific column to show an error message “Please select a different Team”. (last section of the code below to see where to place this).

The code below can be amended to be a Choice column rather than a lookup by changing the getTagFromIdentifierAndTitle(“select”,”Lookup”,”Team Name Required Field”); to your column.

<script>

function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == “” || tempString.indexOf(identifier) == tempString.length – len)) {
return tags[i];
}
}
return null;
}

function PreSaveAction(){
var oTeamName = getTagFromIdentifierAndTitle(“select”,”Lookup”,”Team Name Required Field”);
var blockName = oTeamName.options[oTeamName.selectedIndex].text;

if(blockName.indexOf(‘–Choose a Team–‘)==0||blockName.indexOf(‘Human Resources’)==0){
alert(“Please select a different Team”);
$(“#tname-error”).text(“Please select a different Team”);
return false;
}
return true;
}

</script>

<td width=”400px” valign=”top” class=”ms-formbody”>
<SharePoint:FormField …….
<span id=”tname-error”></span>
<SharePoint:FieldDescription ……

Details below of the tags & identifiers per column type

Column Type Tag Identifier
Single Line of Text input text
Multiple Line of Text input textarea
Lookup select Lookup

 

 

How to change SharePoint button display text

Changing the text for a button within SharePoint can be done through JQuery. Some of the requests I’ve had require this to say ‘Send’, ‘Submit’, ‘Cancel Request’ & ‘Back’.

To begin with, open the form you want this change made, for me, I’d like this on the NewForm. The below form is a copy of the new form that I’ve created in SharePoint Designer.

Buttontext

Edit the NewForm page and add a Script Editor Web Part. Copy the code below and paste in the script editor Web Part.  If you’d like the save value to be something other than save, make sure you change the part where I’ve written ‘Send’.

<script>
$(document).ready(function()
{
$(“input[value$=’Save’]”).attr(‘value’, “Send”);
});
</script>

Buttontext2

Save the changes and stop editing the page. Go back into the NewForm (New Item) page and check the changes.

Buttontext3

You can also edit the form in SharePoint Designer and place the code on the form that way.