none
jQuery, setting Person or Group and Choice field values

    Question

  • I am trying to set some default values for fields using jQuery.  I have the single line of text and multiple line of text fields down but am having trouble setting the Person or Group and Choice fields.  The following works great:

      $('input[title=My Single Line of Text Field]').val(slotField);
      $('textarea[title=My Multiple Line of Text Field]').val(mlotField);
    

    I've tried input, textarea, and select with the Person or Group fields but nothing results in a value placed in the field.  For Choice fields, the following works:

      $('select[title=My Choice Field]').val('Choice Value 1');
    
    But I really want to be able to set the index value or the dropdown so I can make my code more generic and flexible.  Any help would be appreciated.

    Wednesday, March 02, 2011 4:00 PM

Answers

  • Hi Mikey52,

     

    Thanks for your post.

    As I understand, you want to set Person or Choice field value  using JQuery.

     

    Now, let’s have a look at the choice field, after you can a choice field to the list, the source code like this: (My Demo: column title: Region.)

    <SPAN>

    <SELECT class=ms-RadioText title=Region>

    <OPTION selected value=West>West</OPTION>

    <OPTION value=North>North</OPTION>

    <OPTION value=South>South</OPTION>

    <OPTION value=East>East</OPTION>

    </SELECT><BR>

    </SPAN>

     

    And here is the JQuery code :

    <script type = "text/javascript">

    function SetChoiceValue()

    {

                    var selects=$('select[title="Region"]');

                    selects.children().first().removeAttr('selected');

                    selects.children('option[value="North"]').attr('selected','selected');

    }

    </script>

    <input type="button" id="btnSetChoice" value="Change Choice" onclick="SetChoiceValue()" /> <br />

     

    At the same time, have to figure out to set person or group value using JQuery?

     

    Thanks,

    Wayne

    TechNet Subscriber Support in forum

    If you have any feedback on our support, please contact tngfb@microsoft.com

    • Marked as answer by KeFang Chen Friday, March 11, 2011 1:21 AM
    Friday, March 04, 2011 3:29 AM

All replies

  • Well, this did the trick for the Choice fields, still struggling with the Person or Group fields though.

     

    $('select[title=My Choice Field]').attr('selectedIndex', 1);
    

    Wednesday, March 02, 2011 6:41 PM
  • Hi Mikey52,

     

    Thanks for your post.

    As I understand, you want to set Person or Choice field value  using JQuery.

     

    Now, let’s have a look at the choice field, after you can a choice field to the list, the source code like this: (My Demo: column title: Region.)

    <SPAN>

    <SELECT class=ms-RadioText title=Region>

    <OPTION selected value=West>West</OPTION>

    <OPTION value=North>North</OPTION>

    <OPTION value=South>South</OPTION>

    <OPTION value=East>East</OPTION>

    </SELECT><BR>

    </SPAN>

     

    And here is the JQuery code :

    <script type = "text/javascript">

    function SetChoiceValue()

    {

                    var selects=$('select[title="Region"]');

                    selects.children().first().removeAttr('selected');

                    selects.children('option[value="North"]').attr('selected','selected');

    }

    </script>

    <input type="button" id="btnSetChoice" value="Change Choice" onclick="SetChoiceValue()" /> <br />

     

    At the same time, have to figure out to set person or group value using JQuery?

     

    Thanks,

    Wayne

    TechNet Subscriber Support in forum

    If you have any feedback on our support, please contact tngfb@microsoft.com

    • Marked as answer by KeFang Chen Friday, March 11, 2011 1:21 AM
    Friday, March 04, 2011 3:29 AM
  • Hi Mikey52,

     

    If it’s a people or group field, you can set the display name to it directly, and it will be validated when you submit the form.

    Here is the sample code:

    function SetPeopleValue()

    {

                    var peopleDiv=$('div[title="People Picker"]');     

                    if(peopleDiv.parent.)

                    peopleDiv.html(                '<DIV>domain\alias</DIV>');

    }

     

    It’s a demo above, if there are more than one People and group fields in the form, you should find the location at first.

    Thanks,

    Wayne

    TechNet Subscriber Support in forum

    If you have any feedback on our support, please contact tngfb@microsoft.com

    • Marked as answer by KeFang Chen Friday, March 11, 2011 1:21 AM
    • Unmarked as answer by KeFang Chen Friday, March 11, 2011 1:21 AM
    Friday, March 04, 2011 5:34 AM
  • Hi Mikey52,

    function SetPeopleValue()

    {

                    var peopleDiv=$('div[title="People Picker"]');     

                    if(peopleDiv.parent.)

                    peopleDiv.html(                '<DIV>domain\alias</DIV>');

    }

    If you have any feedback on our support, please contact tngfb@microsoft.com

    FYI, this did NOT work for me in SharePoint 2013.

    I am still looking for a solution, so please send one if you have one.

    Thanks.

    Mark Kamoski


    -- Mark Kamoski

    Sunday, November 23, 2014 7:51 AM
  • Hi Mikey52,

    If it’s a people or group field, you can set the display name to it directly, and it will be validated when you submit the form.

    Here is the sample code:

    function SetPeopleValue()

    {

                    var peopleDiv=$('div[title="People Picker"]');     

                    if(peopleDiv.parent.)

                    peopleDiv.html(                '<DIV>domain\alias</DIV>');

    }

    It’s a demo above, if there are more than one People and group fields in the form, you should find the location at first.

    Thanks,

    Wayne

    TechNet Subscriber Support in forum

    If you have any feedback on our support, please contact tngfb@microsoft.com

    FYI, this suggested-solution did NOT work for me.

    I am using SharePoint 2013 and a NewDocSet.aspx page and a custom Content Type.

    Note that this suggested-solution did, in fact, force the HTML to change as-noted, but upon Save of the form SharePoint complains that the data entered is not valid and the form does not save.

    So far, I have tried several web-suggested solutions but none work.

    Does anyone have any other solutions?

    Please advise.

    Thanks.

    Mark Kamoski


    -- Mark Kamoski

    Monday, November 24, 2014 4:24 PM