0

Please tell me how to clear the value PeoplePicker in JavaScript (JQuery)? Tried a lot of examples, nothing works.

$("input[id*='Author']").val('');
$("div[id*='Author']").attr('description','');
$("div[id*='Author']").attr('displaytext','');
$("div[id*='Author']").attr('key','');

2 Answers 2

1

I don't see what you are trying to accomplish as Author is the internal name for the field Created By but here goes.

<script language="javascript" type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="../_layouts/15/clienttemplates.js"></script>
<script src="../_layouts/15/clientforms.js"></script>
<script src="../_layouts/15/clientpeoplepicker.js"></script>
<script src="../_layouts/15/autofill.js"></script>
<script language="javascript" type="text/javascript">

/*Finds and deletes the resolved users in the people picker control "Test" if there are more than zero user present.*/ 

$(document).ready(function() {
peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[Title*="Test"]')[0].id];
    if (peoplePicker.TotalUserCount > 0) {
        peoplePicker.DeleteProcessedUser();
    }
});
</script>
6
  • @Cristoffer Failed to get the "TotalUserCount" property of a reference whose value is not defined or is NULL Commented Apr 25, 2018 at 9:13
  • Think i missed out 2 references.. try again. Or are you trying to manipulate the created by field? The field need to be in the actual form. If the reference is null = field not found. Commented Apr 25, 2018 at 9:14
  • @Cristoffer Tried different options, even created a field people picker "Test", the same mistake "Failed to get the TotalUserCount" property of a reference whose value is not defined or is NULL" Commented Apr 25, 2018 at 9:38
  • Can you debug? What SharePoint version? Should have specified that this is for 2013, hence the reference to _layouts/15. Commented Apr 25, 2018 at 9:58
  • Reference it's Ok, SharePoint 2013 Ent., debug only same error... Commented Apr 25, 2018 at 10:04
0

Following scripts works for me for ClientPeoplePicker. The DeleteProcessedUser method requires span element of resolved user.

Definition:

function removeAllUsers(peoplePickerId){
    var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict["ctl00_PlaceHolderMain_" + peoplePickerId + '_TopSpan'];

    var selectedUsers = peoplePicker.GetAllUserInfo();
    var userToRemoveKey = selectedUsers[1].Key; 

    var resovledListElmId = peoplePicker.ResolvedListElementId;
    $('#' + resovledListElmId).children().each(function(index, element) {
        peoplePicker.DeleteProcessedUser(element);
    });
}

Usage:

removeAllUsers("PeoplePickerControlName");
1
  • Your code does not understand the connection between my Title field "Author"? The normal column that is created in the list and fill in Newform.aspx, JS code is added via Content Editor. There is not project VS. Commented Apr 25, 2018 at 12:04

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.