SharePoint form: scroll to field as with an anchor tag

I got this request from Cookie:

Is there a way to create an anchor within a NewForm or EditForm? I have a long intake, or it could be long. When I assign the the item to an owner and provide them a link to the form, it would be easier to have an addition to the link with the anchor that links to a specific field in the form. Maybe a script that uses the fieldinternalname and converts that to the anchor name for simplicity.

Place this code in a CEWP below the form in NewForm, EditForm or DispForm:
[javascript]
<script type="text/javascript" src="../../Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
/*
This script is used to be able to scroll to a field based upon a url query string parameter like this:
…/EditForm.aspx?ID=1&focus=FieldInternalNameOfFieldToFocusOn
*/

fields = init_fields();

// If the query string parameter "focus" is set, and a field with that FieldInternalName is found, scroll to that field
$(document).ready(function(){
var q = getQueryParameters();
if(q[‘focus’]!=undefined){
var fin = q[‘focus’];
if(fields[fin]!=undefined){
var offset = $(fields[fin]).offset();
scrollTo(0,offset.top);
}
}
});

// Function to separate each url search string parameters
function getQueryParameters(){
qObj = {};
var urlSearch = window.location.search;
if(urlSearch.length>0){
var qpart = urlSearch.substring(1).split(‘&’);
$.each(qpart,function(i,item){
var splitAgain = item.split(‘=’);
qObj[splitAgain[0]] = splitAgain[1];
});
}
return qObj;
}

// Function to make an object of all tr-tags in the form
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;
}
</script>
[/javascript]

Make your links up like this:
…/EditForm.aspx?ID=1&focus=FieldInternalNameOfFieldToFocusOn

Look here on how to find the FieldInternalName of your field.

Regards
Alexander

Leave a Reply