In SP2010 I used the following JavaScript(JS) in a Content Editor on the NewForm.aspx page. This was used to prepopulate a lookup dropdownlist with a value from the Query String Parameter. Now that I've converted to SP2013, the JavaScript is failing to locate what appears to be a completely different looking Lookup Dropdown in the HTML. Is there a quick fix for this code? Recommendations please!

<p><script type="text/javascript">
// This javascript sets the default value of a lookup field identified 
// by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable
// identified by <<QUERYSTRING VARIABLE NAME>>
// Customize this javascript by replacing <<FIELD DISPLAY NAME>> and 
// <<QUERYSTRING VARIABLE NAME>> with appropriate values.
// Then just paste it into NewForm.aspx inside PlaceHolderMain


function fillDefaultValues() 
//Get's parent URL then substrings to the first querystring value
var qs = parent.location.search.substring(1, parent.location.search.length);
//Separates all values separated with &
var args = qs.split("&");
var vals = new Object();
for (var i=0; i < args.length; i++) 
        var nameVal = args[i].split("=");
        var temp = unescape(nameVal[1]).split('+');
        nameVal[1] = temp.join(' ');
        vals[nameVal[0]] = nameVal[1];
setLookupFromFieldName("Project ID", vals["ProjectID"]);

function setLookupFromFieldName(fieldName, value) 
    if (value == undefined) return;
    var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);

// if theSelect is null, it means that the target list has more than
// 20 items, and the Lookup is being rendered with an input element

if (theSelect == null) 
        var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
        ShowDropdown(theInput.id); //this function is provided by SharePoint and shows the drop down being pulled
        var opt=document.getElementById(theInput.opt);
        setSelectedOption(opt, value);
        OptLoseFocus(opt); //this function is provided by SharePoint 
        setSelectedOption(theSelect, value);

function setSelectedOption(select, value) 
    var opts = select.options;
    var l = opts.length;
    if (select == null) return;
    for (var i=0; i < l; i++) 
            if (opts[i].value == value) 
                  select.selectedIndex = i;
                  return true;
    return false;

function getTagFromIdentifierAndTitle(tagName, identifier, title) 
//Finds returns the drop down list object by locating the tag in HTML
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);

    for (var i=0; i < tags.length; i++)
            var tempString = tags[i].id;
            if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) 
                    return tags[i];
    return null;

1 Answer 1


After some more analysis, I resolved the code above. Looks like the conversion from SP2010 to SP2013 changed the value the javascript was looking for in the dropDownList.

var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);

I changed the Lookup to LookupField and the JavaScript above started working again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.