1

I have two choice fields. When choice field A is equal to Commercial then choice field B should change to Staged.

I've found this hide function and was trying to change it to fit my needs but I'm not good with jQuery.

<script src="/jquery/jquery-1.8.3.min.js">
</script><script src="/jquery/SPUtility.js"></script>
<script>

// wait for the window to load
$(document).ready(function () {
    // Get a single select dropdown field
    var assignedToField = SPUtility.GetSPField('AssignedTo');

    // create a function to change the dropdown value based on AssignedTo's value
    var changeField = function() {
        var assignedToFieldValue = assignedToField.GetValue();
        // Change the Contract Shatus field if the selected value is Commercial
        //if(assignedToFieldValue === 'Commercial') {
        if($(this).val() ==== 'Commercial') {
            SPUtility.HideSPField('Contract Status');
        }
        else {
            SPUtility.ShowSPField('Contract Status');
        }
    };

    // run at startup (for edit form)
    showOrHideField();

    // make sure if the user changes the value we handle it
    $(assignedToField.Dropdown).on('change', showOrHideField);
});</script>

UPDATED CODE STILL NOT WORKING:

<script type="text/javascript" src="/jquery/prototype.js"></script>
<script type="text/javascript" src="/jquery/SPUtility.js"></script>
<script type="text/javascript">
Event.observe(window, 'load', function() 
{
    // Get a single select dropdown field
    var assignedToField = SPUtility.GetSPField('AssignedTo');

    // create a function to change the dropdown value based on AssignedTo's value 
    var changeField = function() {
        var assignedToFieldValue = assignedToField.GetValue();
        // Change the Contract Shatus field if the selected value is Commercial
        if(assignedToFieldValue === 'Commercial') {
        //if($(this).val() ==== 'Commercial') {
            SPUtility.GetSPFieldByInternalName('contractstatus').SetValue('Staged');        }

    };

    // run at startup (for edit form)
    changeField();

    // make sure if the user changes the value we handle it
    $(assignedToField.Dropdown).on('change', changeField);


});</script>
  • In order for that updated code to work, you need to know the internal name of the field and contractstatus (all lowercase) does not look like a typical internal name. Not to say it isn't, it's just the first thing my eye goes to when looking at your code, especially because in your first example you refer to the field as Contract Status (with caps and a space). That would lead me to think the internal name of the field is ContractStatus or Contract_x0020_Status. – Dylan Cristy Jan 22 '16 at 20:56
  • To discover the internal name of the field, go to the list settings, and click on the column name as if you were going to edit the column. At the end of the URL, you will see &Field= and then the internal name of the field. – Dylan Cristy Jan 22 '16 at 20:58
0

Continuing with SPUtility, you can set a field value by:

SPUtility.GetSPFieldByInternalName('ChoiceFieldB').SetValue('Staged');

Note that if the choice list is a lookup, then you have to set the value via the ID of the choice from the source list. I.E.

SPUtility.GetSPFieldByInternalName('ChoiceFieldB').SetValue(2);

  • I've updated the code and still cant seem to get it to work...nor does it seem like I can properly ask a question....Heck of a day! – Sherri Jan 22 '16 at 20:45
  • Don't worry, been there! I have a feeling you should use a double equals in place of a triple equals, to compare only the value, rather than value and type. – George Grainger Jan 22 '16 at 21:03
0

Try something like this using only jQuery -

<script src="/jquery/jquery-1.8.3.min.js"></script>
<script> 
    $(document).ready(function(){
        $("select[title='AssignedTo']").change(function(){
            var selected = $(this).find("option:selected").text();
            if(selected == "Commercial") {
               $("select[title='Contract Status']").val('Staged');
            }
        });
    });
</script>

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.