17.06.2010 Updated code to support this heading script: Headings in SharePoint Forms – jQuery
Have you ever wanted to hide rows from your DispForm if they are empty?
As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):
The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.
To begin with NewForm looks like this:
Add a CEWP below your DispForm list-form like this:
With this code:
$("td.ms-formbody").each(function(){ // Trim off all white spaces var val = $(this).text().replace(/\s|xA0/g,''); // Check the string length - if it's 0 hide the field // If it is not a heading - hide it if($(this).parents().html().match('FieldName="#H#')==null){ if(val.length==0){ $(this).parents('tr:first').hide(); } } });
Now your DispForm should look like this:
If you want to hide the table row based on other criteria, adapt the check in line 7 to fit your need – either it’s by length or it’s by string comparison.
Regards
Alexander
Nice, this can be useful for lists wiht many columns for ex. Thanks Alexander!
— http://www.sharepointdesigners.net —
Hi Alexander:
I am using this “Hide” script on many DispForms and it’s a godsend!
Do you know if the script can be changed to be able to print to PDF and still hide the blanks fields? If I use the browser print function, the fields are hidden but if I print to PDF, the hidden fields appear.
Thanks-
Charlie Epes
Hi,
Not sure if this does any difference, but try to change line 8 like this
Alexander
Hi Alexander,
Do you know how I can hide certain fields in DispForm? Not necessarily empty fields, but fields I don’t want the public to see at all.
Thank you!
Hi,
Have you looked at this article:
Dynamic expand/collapse fields or array of fields.
In this code you should find your answer – if not, let me know and i will show you.
Alexander
Hi,
That’s almost what I need, except that I don’t need the rule to be conditional. There are some fields that I don’t want them to appear at all.
Thanks
Hi,
I trimmed off the excess code:
Alexander
It just works! Thank you Alexander!
Hi Thiago,
I have tried this script provided by alex but nothing is hidden :s
anything u can mention which you might have changed in the script or smt to be carefl about
also, did u use this scrit in the add and edit forms
Thanks in advance
Julia
Alexandar, i’ve been using a lot of your example and they are great! I’m having a hard time making this work in DispForm.aspx.
I’ve got a lookup column in the newform, but don’t know how to obtain the value in the dispform.
I want it to be like this…
If lookupfield = “New” then
hide field1
hide field2
If lookupfield = “Old” then
show field1
show field2
Can you help me with that?
Hi,
To get the value from a field in DispForm (replace “Dummy2” with the FieldInternalName of the field):
Alexander
Alexandar… I’ve tried a number of combinations of code and it isn’t creating an alert… I can’t paste the code as it doesn’t seem to go through and show up on the comments.
Maybe this will work without the greater than less than
script type=”text/javascript” src=”/Documents/JavaScript/jquery-1.3.2.min.js”/script
script type=”text/javascript”>
var fieldVal = $(fields[“Category”]).find(“.ms-formbody”).text();
alert(fieldVal);
/script
Alexander
I tried this below, and it doesn’t work. I’ve also tried a number of combinations… (I know i’m the URL to jquery is corect as it is working fine in other code i’m doing.
var fieldVal = $(fields[“Category”]).find(“.ms-formbody”).text();
alert(fieldVal);
Sorry, i have not given you the full story here. The function init_fields() were not included in the example…
Read here how to post code in comments
Alexander
Thank you! Your code works great and displays exactly correct in the message box, but I can’t get it to work in an if statement… I’ve tried it two ways.
1
2
You came close with number 2. Modify the if statement like this:
Alexander
I appreciate all your help… Unfortunately it still doesn’t work.. I’m making sure everything is case sensitive and still no pop up is displayed. I’ve added alert(fieldval) and it does display “New User” without the quotes, but the other alert “New User is actually selected” never displays…
There must be some whitespace behind the value, try to alert it like this:
Alexander
This code should work:
Alexander
Alexander that did it! Thank you soo much… I really appreciate your help!!
Would this code also work in the EditForm.aspx when the field is a lookup column?
None of this works for me. Have copied it into a CEWP and reference the JS and tested it is working. But, nothing hides! Help!
Hi,
Have you remembered to put the CEWP below the list form?
Is the form customized? – look here
Alexander
Alex, the solution you provided for Brandon to display the value of a field in dispForm.aspx worked for me too. You are simply awesome. Thank you for helping all of us. Keep up the good work.
Hi,
Thank you, and I’m glad you could use the code.
Alexander
Would this code also work in the customized EditForm.aspx and customized DispForm.aspx?
Is there a way to modify the code so that in addition to hiding all empty fields, it also hides fields that have a field internal name that starts with two dash characters “–” (even if that field contains data).
$(“td.ms-formbody”).each(function(){
// Trim off all white spaces
var val = $(this).text().replace(/s|xA0/g,”);
// Check the string length – if it’s 0 hide the field
if(val.length==0){
$(this).parents(‘tr:first’).hide();
}
});
Hi,
When combining this functionality with the “headings” script the headings get hidden since they are blank :(.
Can we overcome this by hiding all blank fields except the ones with specific field name (or the ones that start with #H# for your first headings method), and if so how?
Thanks in advance,
Kat
Code updated.
Alexander
Once again ….
Thank you!!!
Kat
Hi, How do I hide hide checkboxes, radiobuttons etc if they are not selected? Thanks
Hi,
This script is all about DispForm and hiding checkboxes and radiobuttons should work in the same way as other columns.
Alexander
Hi Alexander, I wasnt very clear. I have a Yes/No checkbox box on the form and I want to it hide when set as ‘no’ (not checked). What additional code do I need to add on line 7 as it is currently visible on the DispForm even when all other empty fields are hidden?
Thanks
Hi,
The Yes/No check box is special in the way that NOT selected results in a “No” and not an empty string.
Adapt line 7 like this:
Alexander
Great! thanks for the help 🙂
Thanks Alexander, you’re the best!
I’m back to bug you again. I am having a few issues trying to modify your scripts. Your scripts work fine as designed, it is only when I am trying to modify. for this one I have tried to using with the new accordian script, but it will not hide the empty fields. I added an alert to return the val, and I get a blank alert for each empty field but it will not hide. So I changed the one line to this
[sourcecode]
if(val.length==’null’ || val.length==0 || val.length==’ ‘){
still no luck, so I am guessing it has something to do with how the fields are returned with the accordian script. Any hints? This is not a deal breaker but would be helpful.
I am so sorry. I figured it out again. It was the script order. If I put the hide empty fields after the accordian it worked. Sorry to bother.
I got this to work on my site… however it also my hides my Picture field (I am using this on a Contact list). I think I just need an If statement right before for the hide command checking to see if the FieldName is “Picture”. I can seem to figure out the correct syntax to do this. Any help would be greatly appreciated.
Hi!
The script works perfect. But i need some change. I want to leave one specific row (we name it ‘Zugewiesen’ be visible, even if its empty. How can this be done?
thanks
Hi, Sorry for the late reply, but if you still look for the answer:
Alexander
Hi Alexander,
The script is not working for me. I have copied the source to a text file, uploaded to a document library and copied the location of the file and pasted inside a content editor web part on the editdispform. I am using SharePoint 2010. Does that matter at all? Do I need to add the jquery library reference above the first line of code?
$(“td.ms-formbody”).each(function(){
// Trim off all white spaces
var val = $(this).text().replace(/s|xA0/g,”);
// Check the string length – if it’s 0 hide the field
// If it is not a heading – hide it
if($(this).parents().html().match(‘FieldName=”#H#’)==null){
if(val.length==0){
$(this).parents(‘tr:first’).hide();
}
}
});
Hi,
You should wrap it like this:
Change the script “src” to the jQuery file.
Alexander
Thanks Alexander! It worked perfectly.
Best Regards,
Brian
Hi Alexander,
I was trying to customize your script to only hide a with 0 characters after a “:” character. This is because my field names are showing, so even if the field value was left blank, there is still the field name text in the . I tried the script below but it is not working. Do you have any suggestions or corrections to my code?
$(“td.ms-vb-tall”).each(function(){
var val = $(this).text().replace(/s|xA0/g,”);
var afterSemi = val.substr(val.indexOf(“:”) + 1);
if(afterSemi.length==0){
$(this).parents(‘tr:first’).remove();
}
});
Just a follow up to my post…some of my text did not post so it does not read correctly… an updated post below…
I was trying to customize your script to only hide a tr with 0 characters after a “:” character. This is because my field names are showing, so even if the field value was left blank, there is still the field name text in the td. I tried the script below but it is not working. Do you have any suggestions or corrections to my code?
Hi Alexander,
you have a error in your regular expression,
in line 3 you have:
var val = $(this).text().replace(/s|xA0/g,”);
should be:
var val = $(this).text().replace(/\s|xA0/g,”);
Anyway, do you any code to do the same in editform.aspx?
Hi,
I have fixed the regex – thanks.
To do something like this for EditForm, you must refer spjs-utility.js and use a code like this:
This is written “freehand” and not tested, but should work for text / choice columns.
Alexander
Hi Alex
many thanks for the script and explanations
I have tried using the script provided to hide the empty rown on the display page, but all the fields and headers disappeared, even the ones which had content..would you have an idea of why am I getting that behavior
Also,if I wanna use one of your scripts on the edit or add forms, would I have to change something?
Thanks in advance
Julia
I am on sharepoint online, (team site
hi Alex
I found this slightly similar code on another side which worked well:
$(document).ready(function() {
$(“td.ms-formbody”).each(function(){
// Trim off all white spaces
var val = $(this).text().replace(/\s|\xA0/g,”);
// Check the string length – if it’s 0 hide the field
// If it is not a heading – hide it
if($(this).parents().html().match(‘FieldName=”#H#’)==null){
if(val.length==0){
$(this).parents(‘tr:first’).hide();
}
}
});
});
Can you please point out and explain why this one worked and the one we have here didn’t..i would like to understand why so as to be able to use correctly the rest of the scripts u are providing on your side
Many thanks in advance
Hi,
The reason the last one worked must have to do with the wrapping with $(document).ready.
I suspect your CEWP is not placed below the form web part? – this would explain why the first one did not work and the second did.
This code will never work in NewForm or EditForm as all it does is to look at the text in the “td” – and if empty – hide the row.
Alexander
Thanks Alex for the response
the CEWP was below the web part in both cases..
when I didn’t have the wrapping with $(document).ready , all fields and header disappeared, that’s what I found weird
also, when I try the other scripts, even with the wrapping with $(document).ready, I don’t get the expected behavior…would it have smt to do with the fact that I am sharepoint online?
Also, can you please point out to how to adopt the scripts for add & edit if possible
Thank You
Julia
Not sure if this will be useful to somebody, but this is what I had resorted to for hiding fields conditionally..Unfortunately the above didn’t work for me 🙁 I wish it did so as I couldreadily use the rest of the scripts
$(document).ready(function(){
var selected = $(“select[title = ‘Field1’]”).val();
if(selected == “scheduled”) {
$(“TABLE.ms-formtable>TBODY>TR:eq(2)”).css(‘display’, ‘none’);
}
});
Hi,
Have you looked at the DFFS solution? You find it here: https://spjsblog.com/2013/08/12/dynamic-forms-for-sharepoint-v3/
You can also find information regarding showing or hiding fields in for example this article: https://spjsblog.com/2009/11/28/dynamic-expandcollapse-fields-or-array-of-fields-by-multi-select/
Alexander
Thanks Alex , much appreciated !
I wanted to see if you would have an idea how to resolve the following
“I am uploading a script on a CEWP which uses JQUERY. The code does not execute on the first time It is loaded in IE. as pop up bow appears at the bottom of the page pormting the end user if he wants to allow blocked content..only when checked, that the page reloads..and the script gets executed
Is there a way to avoid this and have the blocked content enabled form the start”
Thanks !
Julia
Hi,
If you are on https you must only refer scripts from https sources.
Alexander
That did it!..i was an HTTP link..now I am using a protocol-relative url.
Thank you one more time Alex ! highly appreciate your prompt response and help
Julia
Hi,Alexander Bautz
I have tried the below script to hide the column in dispform.aspx which is null value but didn’t work.
$(document).ready(function() {
$(“td.ms-formbody”).each(function(){
// Trim off all white spaces
var val = $(this).text().replace(/\s|\xA0/g,”);
// Check the string length – if it’s 0 hide the field
// If it is not a heading – hide it
if($(this).parents().html().match(‘FieldName=”#H#’)==null){
if(val.length==0){
$(this).parents(‘tr:first’).hide();
}
}
});
});
Also tried without $(document).ready(function() {
it also not works , I have added script into txt file and give link into CEWP.
Pls help
Hi,
Which version of SharePoint are you running?
Try using this solution: https://spjsblog.com/2009/10/15/hide-empty-rows-in-dispform/#comment-7796
Alexander
I am using SharePoint 2010
Have you tried the solution linked in mu previous comment?
Alexander
Yes, I have added the following script which you have mentioned.
$(“td.ms-formbody”).each(function(){
// Trim off all white spaces
var val = $(this).text().replace(/\s|\xA0/g,”);
// Check the string length – if it’s 0 hide the field
// If it is not a heading – hide it
if($(this).parents().html().match(‘FieldName=”#H#’)==null){
if(val.length==0){
$(this).parents(‘tr:first’).hide();
}
}
});
});
But it doesn’t work in dispform.aspx
You should try using the method I describe in the comment I linked to: https://spjsblog.com/2009/10/15/hide-empty-rows-in-dispform/#comment-7796
Refer spjs-utility.js and use getFieldValue, and the code from the comment.
Alexander
Yes, the script was working now after creating the customize form of display from.
I think the script will work under the customize form
Hi Alexander,
Thank you for this wonderful post!
Unfortunately, the code ain’t working for me…
I tried to implement it on SP 2010 dispform.aspx
I added this code in a CEWP under the list WP in dipform.aspx
$(“td.ms-formbody”).each(function(){
// Trim off all white spaces
var val = $(this).text().replace(/\s|xA0/g,”);
// Check the string length – if it’s 0 hide the field
// If it is not a heading – hide it
if($(this).parents().html().match(‘FieldName=”#H#’)==null){
if(val.length==0){
$(this).parents(‘tr:first’).hide();
}
}
});
Is there’s something I’m doing wrong?