JavaScript to hide SharePoint title column fields in list view

Sometimes you want to show a list on your homepage.
You can do that by adding the webpart to the page. But what is also does is show the headers: the column titles.

For example, if you add the announcements with some own field, you don’t need those headers. You could create a walkaround by adding a content-by-query.

This code (by http://snook.ca/ and http://robertnyman.com/ helps you by hiding those column titles. Just paste it in a content editor on the page.

<script type="text/javascript" language="javascript">
_spBodyOnLoadFunctionNames.push("HideHeaders"); 
function HideHeaders() 
{
  var elements = getElementsByClassName(document, "td", "ms-gb"); 
  var elem;
  for(var i=0;i< elements. length;i++) 
   { 
     elem = elements&#91;i&#93;; 
     elem.childNodes&#91;3&#93;.style.display = "none";
     elem.childNodes&#91;4&#93;.nodeValue = elem.childNodes&#91;4&#93;.nodeValue.replace(':', '');
   } 
  elements = getElementsByClassName(document, "td", "ms-gb2"); 
  for(var i=0;i< elements.length;i++)
   {
     elem = elements&#91;i&#93;; 
     elem.childNodes&#91;3&#93;.style.display = "none"; 
     elem.childNodes&#91;4&#93;.nodeValue = elem.childNodes&#91;4&#93;.nodeValue.replace(':', '');
     for(var i=0;i< elements.length;i++)    { 
     elem = elements&#91;i&#93;; 
     elem.style.display = "none"; 
   } 
} 

function getElementsByClassName(oElm, strTagName, strClassName){ 
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName); 
    var arrReturnElements = new Array(); 
    strClassName = strClassName.replace(/\-/g, "\\-"); 
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)"); 
    var oElement; 
    for(var i=0; i< arrElements.length; i++){ 
        oElement = arrElements&#91;i&#93;; 
        if(oRegExp.test(oElement.className)){ 
            arrReturnElements.push(oElement); 
        } 
    } 
    return (arrReturnElements) 
} 
</script>

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.