Hiding fields on NewForm.aspx and EditForm.aspx

First open entry form (NewForm.aspx / EditForm.aspx / DispForm.aspx) in advanced mode in sharepoint designer. Then search for tag :

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

Now copy & paste this JavaScript code after this tag :

<script type="text/ecmascript">

 ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
 function runCode()
 {
  HideField("Expose to Customer / Partner",true);
                HideField("Reported By",true);              
                HideField("Reported Date",true);
 }

 // Function To Hide Fields
   function HideField(title,hide){
     var header_h3=document.getElementsByTagName("h3") ;
     for(var i = 0; i <header_h3.length; i++)
     {
       var el = header_h3[i];
       var foundField ;
       if(el.className=="ms-standardheader")
       {
         for(var j=0; j<el.childNodes.length; j++)
         {
           var mHead=title+"<SPAN class=ms-formvalidation> *</SPAN>";
           if(el.childNodes[j].innerHTML == title || el.childNodes[j].nodeValue == title || el.childNodes[j].innerHTML==mHead)
           {
             var elRow = el.parentNode.parentNode ;
             if(hide==true)
             {
              elRow.style.display = "none"; //and hide the row
             }
             else
             {
              elRow.style.display = "visible"; //and show the row
             }
             foundField = true ;
             break;
           }
         }
       }
       if(foundField)
        break ;
     }
   }
 
</script>

Comments

Anonymous said…
Hi,

Great post.

Having hidden all the fields (Workspace, Recurrence and All Day Event, which cannot be hidden using the content definition), I now have quite a large space at the bottom of the new/edit/display forms.

Is there any way of changing form height to compensate for the absence of the fields?

Thanks.
Navaratan said…
Hi Gorav
It”s indeed a good post, helped me a lot.
Navaratan said…
Hi Gorav
It”s indeed a good post, helped me a lot.
Anindya said…
I believe using jQuery to hide fields is easier, atleast in terms of lines of code!

jQuery can target HTML elements in an easier way.
Ratikanta said…
What about the fields in forms ,those are build in Infopath form services ?
Gaurav Goyal said…
To hide fields in infopath : http://the-north.com/sharepoint/post/2012/06/12/SharePoint-and-Infopath-2010-Conditionally-Hide-Form-Fields.aspx
geetha said…
Hi Many thanks for your post it helped me a lot. is there a way to hide fields based on user input
Unknown said…
Thank you very much. Of the many suggestions I tried this one was easy, clear and worked well.

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

Change Language for current user with JSOM in SharePoint Online

SharePoint 2010 CSS Chart