Thursday, October 15, 2009

Hiding field in SharePoint through JavaScript

Hi All,

Many times we require to hide specific field in new form, disp form and edit form. This time we will do it with the help of javascript. We can do the same thing with the help of SharePoint API. However we will cover here that how to achieve the same with the help of JavaScript.

Let us go to calendar and click on new. You will now be on Newform.aspx. We can see here one field named “WorkSpace”. If we don’t want user to create any workspace here, then we can hide this field with the help of JavaScript code.



To hide a specific field, insert one content editor web part on the page and then just copy and paste the following code. Insert this web part below the new form web part.

<script type="text/javascript">
function HideField(title){
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++)
{
if(el.childNodes[j].innerHTML == title || el.childNodes[j].nodeValue == title)
{
var elRow = el.parentNode.parentNode ;
elRow.style.display = "none"; //and hide the row
foundField = true ;
break;
}
}
}
if(foundField)
break ;
}
}

HideField("Workspace");

</script>


After you’ve inserted this code, just look at the result shown below.



That’s it. Your job is done. You can apply the same code in disp and edit form as well.

12 comments:

Anonymous said...

Where should we add this Code?

SharePoint Kings said...

you can write this code on
newform.aspx/Editform.aspx/dispform.aspx means on List forms.

SharePoint Kings said...

Refer this link http://www.sharepointkings.com/2008/05/how-to-edit-list-forms-like-newformaspx.html to know how to edit list pages.

Unknown said...

Can this code be placed in a webpart with an audience enabled? Want items to be hidden for certain SharePoint groups.

SharePoint Kings said...

Yes Brad, you can use this in webpart.

Priyanka said...

Hi,

Thanks for the article..
However,after applying the fix,The field is still visible in edit form page.
Any suggestions on why this is happening?

Thanks

SharePoint Kings said...

Priyanka,

just check whether javascript is working (by putting alert).

if its working then check the name of the control in view source and use that name to hide.

marc said...

How could you format this to hide a field based upon a value in a dropdown?

SharePoint Kings said...

marc,

refer this link
http://www.sharepointkings.com/2010/05/required-field-using-jquery-in-newform.html

this may help you

Clem said...

Nice post. I realize it's a year later... Is it possible to adapt this to SharePoint 2010 (Modal dialogs)?

SharePoint Kings said...

@Clem, yes its possible

its a normal SharePoint page only which SP2010 is opening in model dialog.

if you found URL and try to open it in normal window then also it opens. so you can easily apply JavaScript on the same.

Anonymous said...

almost three years later and this snippet works like a charm. I added it to some forms today.

Thanks!




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com