Set a People Picker’s Value on a Form

Following up on my recent post about getting the current value of a People Picker on a form, today I built the JavaScript to set the value, and here it is. As usual, I’ve left in my debugging code in case you want to see how it works.

// setPickerInputElement: Set a People Picker's value using its identifier to find it in the page
// Arguments:
//                            identifier:            The identifier for the instance of the fieldName (ff1, ff2, etc.)
//                            value:                    Set the fieldName to this value
//

function setPickerInputElement(identifier, value) {
  var tags = document.getElementsByTagName('DIV');

  for (var i=0; i < tags.length; i++) {
    var tempString = tags&#91;i&#93;.id;
    //alert('tags&#91;' + i + '&#93;.id = ' + tempString);
    if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0)){
      //alert('HIT for ' + identifier + ' id=' + tags[i].id + ' value=' + tags[i].value);
      tags[i].innerHTML = value;
      break;
    }
  }
}

Similar Posts

19 Comments

  1. Hi Marc, I modified your code because it was not working, you set the “innerHTML” of an upper “DIV” element, which contains lower “” elements. When updating the 2nd “” element, the code is working.
    Here is my ( working code ):

    //————————————————————————
    // SET PEOPLE PICKER FIELD USING “USERNAME”
    //————————————————————————
    // 2020-02-04 MODIFICATION BY STEFFEN HAASE | [email protected]
    //
    // identifier: The identifier for the instance of the fieldName (ff1, ff2, etc.)
    // value: Set the fieldName to this value
    //————————————————————————
    setPickerInputElement(“ff11”, “USERNAME”);

    function setPickerInputElement(identifier, value)
    {
    var tags = document.getElementsByTagName(‘DIV’);

    for (var i=0; i < tags.length; i++)
    {
    var tempString = tags[i].id;

    if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0))
    {
    
      //---------------------------------
      // FIND UNDERLYING <span>
      //---------------------------------
      var div_id = tags[i].id;
    
        $("#"+ div_id ).each(function ()
        {
            var all_span_elements = $(this).find('span');
    
            for (var p=0; p < all_span_elements.length; p++) 
            {
                if ( all_span_elements[p].id == "content" )
                {
                    all_span_elements[p].innerHTML = value;
                }
            }
    
        });                   
    
      break;
    }
    

    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.