Find and hide fields in NewForm, DispForm and EditForm in both IE and Firefox

This post is old… you will find other ways to do all this in newer posts – Alexander

This post will talk about how to use javascript to find a form-field in av SharePoint list form (NewForm, DispForm and EditForm) from its unique FieldInternalName.

When you have found it you can do almost anything you want with it – hide it, make it readOnly, return the value of the field or validate the input. I will come back to this in future posts in this “Series”.

The method i will talk about here is not new – i believe the guys at CleverWorkarounds posted it first, or was it here – I’m not really sure (if you do know who to credit – please let me know!).

Almost everyone uses a method where they search for a field by the fields DisplayName, but this is not a very robust way of doing it as the displayName can easily be altered – and thereby breaking the script.

It has also been talked about using the Id of the field as reference – the id one would believe to be unique – but it is NOT! If you change the column order in the list form – the id changes – it has a reference to the column order in the id.

This leaves only one suitable way to reference fields – by it’s FieldInternalName.
To find the FieldInternalName you can view the source of the page and search for your DisplayName.

One thing to remember when using FieldInternalName is that it newer changes – and therefore the FieldInternalName could end up totally different from the DisplayName.

When creating fields all spaces in the fieldName will be replased with _x0020_, and special characters with its equivalent hex-code – therefore create a proper fieldname first – then edit it to add spaces and special characters. By doing this your FieldInternalName will be readable and makes sense in your code.

The script i have created uses the FieldInternalName to uniquely identify the field, but it works ONLY in Internet Explorer because it searches for a “Comment-tag” which is not recognized in Firefox as a valid HTML tag:

<script type="text/javascript">
function findacontrol(FieldInternalName) {
var arr = document.getElementsByTagName('!');
	for (var i=0;i < arr.length; i++ ) {
		if (arr[i].innerHTML.match('FieldInternalName="' + FieldInternalName + '"')) {
			if(window.location.href.indexOf('DispForm.aspx')>0) {
				return arr[i].parentNode.childNodes[1];
			}else{
				return arr[i].parentNode.childNodes[1].childNodes[0];
			}
		}
	}
}
</script>

I’m not using Firefox myself, but realized that it would be nice to have this work in Firefox to – almost all my scripts depends on this method of finding fields.

I therefore made a similar script that works on both browsers, but it’s not as sleek as the one for IE only.

The main reason it has to be different is that Firefox does not recognize <!– the comment- tag –>[/javascript] as a valid HTML tag and therefore one have to use the td-tag to search the code. The disadvantage of this approach is that there are far more td-tags than comments in the code and it must loop trough more code in the hunt for “your field”.

One other thing Firefox does different than IE, is childNodes and nextSibling. Firefox treats line breaks in the code as a node and therefore the depth of childNodes in the script must be different for the two.

Script for both IE and Firefox:

<script type="text/javascript">
function findacontrolCrossBrowser(FieldInternalName) {
var arr = document.getElementsByTagName(‘td’);
for (var i=0;i < arr.length; i++ ) {
if (arr[i].innerHTML.match(‘FieldInternalName="’ + FieldInternalName + ‘"’) && arr[i].className == ‘ms-formbody’) {
if(window.location.href.indexOf(‘DispForm.aspx’)>0) {
return arr[i].parentNode.childNodes[1];
}else{
if(arr[i].childNodes[1].nodeType==1) {
// Assumes it is IE
return arr[i].childNodes[1].childNodes[0];
}else if(arr[i].childNodes[1].nodeType==8) {
// Assumes it is Firefox
return arr[i].childNodes[3].childNodes[0];
}
}
}
}
}
</script>
[/javascript]

How to add webPart to NewForm, DispForm or EditForm? Click here.

One possible use of this script is to hide a single field, or a field array:

function hideFieldsArray() {
var array = [‘Field1’, ‘Field2’];
for (x in array)
{
xField = findacontrolCrossBrowser(array[x]);
if(xField!=null)
{
if(window.location.href.indexOf(‘DispForm.aspx’)>0)
{
xField.parentNode.parentNode.style.display="none";
}else{
xField.parentNode.parentNode.parentNode.style.display="none";
}
}
}
}
</script>
[/javascript]

A fully functional setup of this script would be to add a CEWP below the form in for instance NewForm.aspx, and add the following script to it – of course change ‘Field1’ and ‘Field2’ with your FieldInternalName:

&lt;script type=&quot;text/javascript&quot;&gt;
hideFieldsArray();

function hideFieldsArray() {
var array = ['Field1', 'Field2'];
for (x in array)
{
xField = findacontrolCrossBrowser(array[x]);
	if(xField!=null)
		{
			if(window.location.href.indexOf('DispForm.aspx')&gt;0)
			{
				xField.parentNode.parentNode.style.display=&quot;none&quot;;
			}else{
				xField.parentNode.parentNode.parentNode.style.display=&quot;none&quot;;
			}
		}
	}
}

function findacontrolCrossBrowser(FieldInternalName) {
var arr = document.getElementsByTagName('td');
	for (var i=0;i &lt; arr.length; i++ )  {
		if (arr[i].innerHTML.match('FieldInternalName=&quot;' + FieldInternalName + '&quot;') &amp;&amp; arr[i].className == 'ms-formbody') {
			if(window.location.href.indexOf('DispForm.aspx')&gt;0) {
				return arr[i].parentNode.childNodes[1];
			}else{
				if(arr[i].childNodes[1].nodeType==1) {
					// Assumes it is IE
					return arr[i].childNodes[1].childNodes[0];
				}else if(arr[i].childNodes[1].nodeType==8) {
					// Assumes it is Firefox
					return arr[i].childNodes[3].childNodes[0];
				}
			}
		}
	}
}
&lt;/script&gt;

You can also call scripts on pageLoad with:

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("functionName");
</script>
[/javascript]
but then the hiding of the fields will happen after the page has been presented to the use, and will result in a seconds flash of all your “hidden fields” – not good.

I will return with more on how to return the value from almost all field types (Single line of text, Multiple lines of text (Plain text), Choice (dropdown, radio and multi choice), Date and Time, Lookup, Yes/No, Person or Group), how to use and validate the input, and how to dynamically collapse or expand fields based on selections in other fields.

Stay tuned!

Alexander

7 thoughts on “Find and hide fields in NewForm, DispForm and EditForm in both IE and Firefox”

  1. Hello,

    I have implemented this solution with a few minor tweaks, but I am having a weird problem. When I choose to view earlier versions of a list item, it only shows me the most recent fields, even though the list item date created and version number is correct. In the version history list, the changes can be seen, but not when a specific version is selected and displayed. I am playing around with it now to see if I can restore previous versions even though it isn’t being displayed correctly, but I wanted to know if you heard of anything like this.

    Thank you very much!

    1. Hi,
      This is an “old” post that should be updated… If you want to hide fields in a SharePoint form look at this post and it’s comments.

      Regarding your weird problem, i have no solution. Are you sure the problem do not have another explanation? – have you tried to remove the script to see how it behaves without?

      Alexander

    1. Hi,
      This article is old, do it like this in stead:

      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
      <script type="text/javascript">
      var fields = init_fields_v2();
      
      // Hide the field with internalname "MyFieldInternalName"
      $(fields['MyFieldInternalName']).hide();
      
      
      function init_fields_v2(){
      	var res, myMatch, disp, fin, type;
      	res = {};
      	$("td.ms-formbody").each(function(){
      	myMatch = $(this).html().match(/FieldName="(.+)"s+FieldInternalName="(.+)"s+FieldType="(.+)"s+/);	
      		if(myMatch!==null){
      			// Display name
      			disp = myMatch[1];
      			// FieldInternalName
      			fin = myMatch[2];
      			// FieldType
      			type = myMatch[3];
      			if(type=='SPFieldNote'){
      				if($(this).find('script').length>0){
      					type=type+"_HTML";
      				}
      			}
      			if(type=='SPFieldLookup'){
      				if($(this).find('input').length>0){
      					type=type+"_Input";
      				}
      			}
      			// Build object
      			res[fin] = this.parentNode;
      			$(res[fin]).attr('FieldDispName',disp);
      			$(res[fin]).attr('FieldType',type);
      		}		
      	});
      	return res;
      }
      </script>
      

      Put this code BELOW the form (NewForm, DispForm or EditForm).

      Alexander

  2. I did and have it installed thats where i ran into wanting see if there was a better way to hide fields cause i am using an old way i found using the ID which i dont like at all. I saw you had the hide fields option on the dynamic tool and wanted to see if you had a better fix….or if i should try to hide all the fields i want to on the new form with the dynamic tool somehow.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.