2

After making an AJAX request, I have been able to populate fields on a form. However I also need to set the value of a look up column to the same value from the second list (LIST A and B both have a lookup column to the same column in List C)

The below code is the lines I am using to set the fields. Line 1 sets a normal single line field to the correct data however line 2 does not change the lookup (in dropdown format).

I have also double checked that the column internal name is Branch

document.getElementById('ctl00_ctl45_g_aecb3c18_b892_4c95_af79_151459af0ea2_ff11_ctl00_ctl00_TextField').value = $(this).attr('jcvr');
document.getElementById('ctl00_ctl45_g_aecb3c18_b892_4c95_af79_151459af0ea2_ff811_ctl00_Lookup').value = $(this).attr('Branch');
1

So you basically want to load the content of a second list into a HTML Select-Field in your edit form of the first list?

Try to avoid hard code DOM ids. I would prefer using REST and jQuery. And it's better to completely re-build up a select field than trying to alter it:

$.ajax({
    url: '/YOUR_SITE_NAME/_api/web/lists/getbytitle(\'YOUR_LIST_NAME\')/items';,        
    method: "GET",
    async: true,
    headers: {
        "Accept": "application/json; odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function(resultsJSN){
        $('#YOUR_SELECT_DOM_ID').empty();
        for(var i = 0; i < resultsJSN.d.results.length; i++) {
            var titleSTR = resultsJSN.d.results[i].Title;
            var idINT = resultsJSN.d.results[i].ID;
            $('#YOUR_SELECT_DOM_ID').append('<option value="' + idINT +'">' + titleSTR + '</option>')
        }
    },
})

By the way: $('#yourId') is the same as document.getElementById('yourId')

  • Yes however the select-field data comes form a third list. List A is the Form. List B is the user details. List C contains locations. List A and B have the same lookup to List C. Also not sure if this is relevant but the value's (ID's?) in the select-field start at 78. – Roland Nov 17 '15 at 22:26
  • Should also mention that the select-field needs to be ordered in alphabetical order of the text and not by the value. – Roland Nov 17 '15 at 22:51
  • It doesn't matter where the IDs start. To order the results simply add the $orderby statment in the url: *$.ajax({ url: '/YOUR_SITE_NAME/_api/web/lists/getbytitle(\'YOUR_LIST_NAME\')/items&$orderby=Title'* Here you find further information about REST: msdn.microsoft.com/en-us/magazine/dn198245.aspx – Dave Nov 18 '15 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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