Easily Hide Columns on a SharePoint Form with jQuery

This is a really simple little thing. I end up writing little functions like this all the time, and never think much about them. But they are darn useful.

This little function will hide a list column’s row in the form. You might want to do this on a NewForm but not the EditForm, for instance, so setting the column to be hidden may not be a good answer. With this little function, you can hide the column’s row conditionally or on page load – whatever suits your fancy.

Here’s the function. All you need to pass it is the DisplayName of the column.

// Function to hide a column's row in the form
function hideColumn(c) {
  $(".ms-formlabel h3 nobr").filter(function() {
    var thisText = $.trim($(this).clone().children().remove().end().text());
 //   alert("::" + thisText + "::");
    return thisText.indexOf(c) === 0 && thisText.length === c.length;
  }).closest("tr").hide();
}

Then you might call the function like this:

$(document).ready(function() {
  hideColumn("Priority");
});

This will simply hide the column, but any value in it will still be submitted.

For instance, I just added a column to a list called “User Agent String”, set like so:

$("textarea[title='User Agent String']").val(navigator.userAgent);

and then hid it:

hideColumn("User Agent String");

Put it together and what have you got? (Bonus points to anyone who knows the next line in the song.)

$(document).ready(function() {

  // Set the User Agent String
  $("textarea[title='User Agent String']").val(navigator.userAgent);

  // Hide the columns which aren't relevant to the user
  hideColumn("User Agent String");

});

// Function to hide a column's row in the form
function hideColumn(c) {
  $(".ms-formlabel h3 nobr").filter(function() {
    var thisText = $.trim($(this).clone().children().remove().end().text());
 //   alert("::" + thisText + "::");
    return thisText.indexOf(c) === 0 && thisText.length === c.length;
  }).closest("tr").hide();
}

Similar Posts

34 Comments

  1. Thank You very much! There is different markup in on-premise and Cloud forms, so I improved this function a little.

    function getRowByDisplayName(fieldName) {
        var myFieldRow = null;
    
        // check if Form is in Edit or Display mode
        var isDisplayMode = document.location.pathname.indexOf("/DispForm.aspx") > -1;
        var isEditMode = document.location.pathname.indexOf("/EditForm.aspx") > -1;
        var isNewMode = document.location.pathname.indexOf("/NewForm.aspx") > -1;
    
        var rowGetter = "";
        if (isEditMode || isNewMode) {
            rowGetter = ".ms-formlabel span nobr";    /* for Office365 */
            //rowGetter = ".ms-formlabel h3 nobr";    /* on-premise */
        }
        if (isDisplayMode) {
            rowGetter = ".ms-formlabel span";         /* for Office365 */
            //rowGetter = ".ms-formlabel h3";         /* on-premise */
        }
    
        myFieldRow = jQuery(rowGetter).filter(function () {
            var thisText = jQuery.trim(jQuery(this).clone().children().remove().end().text());
            return thisText.indexOf(fieldName) === 0 && thisText.length === fieldName.length;
        }).closest("tr");
    
        return myFieldRow;
    }
    

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.