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