I got this request from Khan:
Alexander,
… Another question I have is how I can display two fields (and their labels) in the same row? I know I am asking too much. But I really hope you can give me some advice o this. Thanks man.
Here is an image of a few random fields put side-by-side:

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.
Add a CEWP below your NewForm/DispForm/EditForm with this code:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/FieldsSideBySide.js"></script> <script type="text/javascript"> // Append fields to the field "User" arrToPutSideBySide = ['StartTime','EndTime']; customSideBySide(arrToPutSideBySide,'User'); // Appends another set of fields to the field "User" arrToPutSideBySide = ['FirstName','MiddleName','LastName']; customSideBySide(arrToPutSideBySide,'User'); // To append the fields to the top use this code: //arrToPutSideBySide = ['FirstName','MiddleName','LastName']; //customSideBySide(arrToPutSideBySide,'',true); </script>
This code is not the full code from the screenshot above. Each horizontal “section” is made with a separate call to the function “customSideBySide()”.
Parameters explained:
- arr: Array or fields to put in one line.
- insertAfterField: If you want to append the fields below another field, insert the FieldInternalName here. Set this parameter to “” if you want to use the next parameter.
- appendToTop: To insert the fields in the top of the form, set it to true. To insert at the bottom of the form, set it to false.
The sourcecode for the file “FieldsSideBySide.js” looks like this:
/* Sharepoint form: Present fields side-by-side
* ---------------------------------------------
* Created by Alexander Bautz
* alexander.bautz@gmail.com
* http://sharepointjavascript.wordpress.com
* v1.0
* LastMod: 26.01.2010
* ---------------------------------------------
* Must include reference to:
* jQuery - http://jquery.com
* ---------------------------------------------
*/
function customSideBySide(arr,insertAfterField,appendToTop){
if(typeof(fields)=='undefined')fields = init_fields();
// Does the field specified as "insertAfterField" exist?
if(insertAfterField!='' && typeof(fields[insertAfterField])=='undefined'){
alert("The FieldInternalName \"" + insertAfterField + "\", which is specified as \"insertAfterField\" does not exist.");
return false;
}
var str = '';
$.each(arrToPutSideBySide,function(i,fin){
// Check if FieldInternalName is correct
if(typeof(fields[fin])=='undefined'){
alert("The FieldInternalName \"" + fin + "\" does not exist.");
return false;
}
// Adapt width of single line and multi lines of text
if($(fields[fin]).html().indexOf('FieldType="SPFieldText"')>0){
$(fields[fin]).find('input').css({'width':'100%'});
}else if($(fields[fin]).html().indexOf('FieldType="SPFieldNote"')>0){
$(fields[fin]).find('textarea').css({'width':'100%'});
}
var tdWidth = Math.round(100/arr.length);
str += "<td class='ms-formbody' style='width:"+tdWidth+"%'>" + $(fields[fin]).find('.ms-formlabel').html() + $(fields[fin]).find('.ms-formbody').html() + "</td>";
// Remove original field
$(fields[fin]).remove();
});
str = "<tr><td colspan='2'><table cellpadding='0' cellspacing='0' style='width:100%'><tr>" + str + "</tr></table></td></tr>";
if(insertAfterField!=''){
$(fields[insertAfterField]).after(str);
}else{
if(appendToTop){
$("table.ms-formtable tbody:first").prepend(str);
}else{
$("table.ms-formtable tbody:first").append(str);
}
}
}
function init_fields(){
var res = {};
$("td.ms-formbody").each(function(){
if($(this).html().indexOf('FieldInternalName="')<0) return;
var start = $(this).html().indexOf('FieldInternalName="')+19;
var stopp = $(this).html().indexOf('FieldType="')-7;
var nm = $(this).html().substring(start,stopp);
res[nm] = this.parentNode;
});
return res;
}
Save as “FieldsSideBySide.js” – mind the file extension, and upload to the script library as shown above.
Ask if something is unclear.
Regards
Alexander
March 20, 2011 at 2:30 am |
Does anyone know how to get the side by side code to work on a multiline enhanced text field? Thanks
August 25, 2011 at 6:18 pm
Hi,
This is not possible due to the complexity of the rich text fields.
Alexander
September 16, 2011 at 7:10 pm |
I cannot get this fieldssidebyside.js to work with Firefox. It works in IE – Can you provide assistance?
September 16, 2011 at 7:12 pm
The error says, “The Field Internal Name does not exist”, but again, it does work in IE
It looks like in Firefox, the last letter of the field internal name is being cut off.
Thanks!
September 16, 2011 at 11:45 pm
Hi,
You need to change the function init_fields() with the one found here: http://sharepointjavascript.wordpress.com/2010/04/07/revised-function-init_fields/
Alexander
November 9, 2011 at 3:16 pm |
This is working great with the exception of date fields. The calendar pop-up is not working. I am not getting any errors, the pop-up just does not display Did anyone else run into this issue? If so, how did you resolve?
November 30, 2011 at 9:56 pm
Hi,
What browser do you use?
Alexander
December 1, 2011 at 5:27 pm
IE 8
December 5, 2011 at 12:04 am
Hi,
I cannot recreate the problem – do you have other scripts in the same page?
Alexander
February 9, 2012 at 9:54 pm |
This will not work on a custom form as there is no FiledInternalName…
The function init_fields needs to be flexible to accomodate this i.e.
function init_fields(type)
February 9, 2012 at 10:25 pm
There is no way you could have a function that identifies fields in all customized forms. The fact that the form is customized by individual users makes it impossible to span them all in one function. If you read the topmost note on the front page of this site, I have given an example on how you can make your own custom “init_fields” function.
Alexander
February 10, 2012 at 2:29 pm
Ahh, thanks. I didn’t see that.
March 2, 2012 at 5:27 pm |
I am receiving the following message: “The FieldInternalName “User”, which is specified as “insertAfterField” does not exist.
Any ideas?
Thanks,
Brian
March 8, 2012 at 9:43 pm
Are you sure the correct FieldInternalName is “User”?
Look here for tips on getting the FieldInternalName
Alexander
March 8, 2012 at 10:28 pm
Thanks Alexander! I will review it. Another Question? I have a list that has over 200 columns and when viewing and editing the contents of the list, I have to scroll through the entire list to review fields that have information in them traversing fields that are blank. The fields are unique for each record and may be populated depending on the record so hiding them is not an option. Is there a way without using SharePoint Designer to dynamically sort fields on a list on New, Edit, and Disp form that have content in them for each unique record?
Your feedback is greatly appreciated.
Regards,
Brian
March 15, 2012 at 11:00 pm
Hi,
I’m not sure I understand. What do you mean by “each unique record”? How would the script know which fields to show or hide?
Please answer by adding a new comment (with the full explanation) in the requests-page
Alexander
March 15, 2012 at 11:34 pm |
Hi Alexander,
First, I got the side by side functionality to work. It was the “Field Internal Name”. Thanks! I have a list that has a large number of records and some of those records do not have values in all of the columns. Some of the fields are blank which is why I called them unique. I am importing a spreadsheet into SharePoint as a list. The data is an output from another system. The issue is that each record is too long to scroll through. When I display a record or edit I have to scroll down through all of these fields in the record that some of them are blank. I am only interested in seeing the ones with content in them. I can’t hide them because while one record may have a column that is empty it maybe populated in another record. I am looking to better display the list record when viewing or editing by not having to scroll an entire record for information. I want the ability to show the record with fields that have content first then the blank fields to appear after them on a sharepoint list.
Thanks,
Brian