Tuesday, February 24, 2009

How to Hide fields in Newform.aspx/editform.aspx

Suppose you don't want to show a column/field in Newform.aspx or Editform.aspx for a list. There are couple of way to hide a column from Newform.aspx/Editform.aspx. I have used the javascript in Content Editor webpart to hide the column in Newform.aspx. I open the Newform.aspx in the browser. By default the "Editpage" option for this page will be disabled. To the URL of the page add "&Toolpaneview=2". You will be able to edit the page. Click on "Add a new webpart" and select "Content Editor Webpart". In the "source view" add the following code :

//
script language="javascript" type="text/javascript">_spBodyOnLoadFunctionNames.push("hideFields");
function hideFields() {var control = getTagFromIdentifierAndTitle("Input","TextField","Misc");control.parentNode.parentNode.parentNode.style.display="none";}
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;}
//
//
The Above code assumes that you have a Textfield named "Misc".


No comments: