0

I have three person field in my list, in new list form I have to auto populated with current user's name only for one field. I wrote the following code. But this code auto populated with current user's name for all three user fields.

<script type="text/javascript"> 
$(document).ready(function() { 
$('div.ms-inputuserfield').text($().SPServices.SPGetCurrentUser({fieldName: "Title",debug: false}));
</script>

Any idea how to fix this?

  • 1
    Can you share your input field code? Have you set the same class name for all three input? – Rajagopal 웃 Mar 2 '18 at 6:38
  • I didn't change anything in markup. Everything is same as default SharePoint. – Sivakumar Piratheeban Mar 2 '18 at 6:58
  • 1
    We don't know what you got in the markup currently. We couldn't help you with the exact solution until you expose your code. – Rajagopal 웃 Mar 2 '18 at 7:13
1

We assume that you have set the same class name for all three elements like this,

<div class="ms-inputuserfield"></div>
<div class="ms-inputuserfield"></div>
<div class="ms-inputuserfield"></div>

when you query by it's class name $('div.ms-inputuserfield') will get the all three elements. So, when you change anything from here will update in all three elements.


Solution 1:

You can update to specific element based on it's index like this,

$('div.ms-inputuserfield').eq(index).text("your text")

Solution 2:

Providing the unique class name to get the exact element that you needed,

<div class="ms-inputuserfield current-user"></div>
<div class="ms-inputuserfield"></div>
<div class="ms-inputuserfield"></div>

And then in your javascript,

$('div.ms-inputuserfield.current-user').text("your text")
1

It is because “$('div.ms-inputuserfield')” finds all attributes with “ms-inputuserfield” class.

Change it to the id of the div:

<script type="text/javascript"> 
$(document).ready(function() { 
$('div#<id of the attribute>').text($().SPServices.SPGetCurrentUser({fieldName: "Title",debug: false}));
});
</script>

Use F12 to get the id:

enter image description here

For example:

$('div#ctl00_m_g_b0ae3ac2_3b68_4235_9a78_7a3d521194e9_ctl00_ctl05_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv').text($().SPServices.SPGetCurrentUser({fieldName: "Title",debug: false}));

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.