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[i].id; //alert('tags[' + i + '].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; } } }
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;
}
}
@Steffen:
This is a really old post, so no surprise thing have changed!
M.