10.25.2012

SharePoint 2007 - Hiding Fields w/ Javascript

I actually found this solution to be a bit more elegant, and more compatible with browsers. Just use jQuery and target the ID of the field OR the attribute value.

<script language="javascript" src="https://mysite.company.org/Code/JS/jQuery/jquery-1.8.2.min.js"></script>
<script language="javascript">
// ==================================================================
// JQUERY
// ==================================================================
$(function(){
  //alert("test");
  $("#ctl00_m_g_6a6807bc_96fc_43e6_bfe3_20b1971aadcc_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").prop("readonly",true);
   $('input[title="Title"]').prop("readonly",true);

});
</script>


=====================================

Making fields hidden should quite frankly be a built in feature of SharePoint. Here are two interesting tricks I found to modify the NewForm.aspx and EditForm.aspx pages, without opening up SharePoint Desinger.

1) Append this to the URL string. This will force the page edit option. &ToolPaneView=2

2) Add a Content Editor Web Part to the page.

3) Paste this javascript code into the web part.


 <script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("hideFields");

function findacontrol(FieldName) {
 var arr = document.getElementsByTagName("!");
 // get all comments
 for (var i=0;i < arr.length; i++ )
 {
 // now match the field name
 if (arr[i].innerHTML.indexOf(FieldName) > 0)
 { return arr[i]; }
 }
}

function hideFields() {
 var control = findacontrol("Title");
 control.parentNode.parentNode.style.display="none";
}
</script>


Sources: