JavaScript logic in Content Editor Web Part(CEWP)

213 Views Asked by At

I am try to hide fields in an edit form base on conditions(values in other fields) in a SharePoint list. The below code works to hide the fields but the logic is not working right now. Using alert I am able to see the different values selected in the field but the conditional statements are not reseting the fldList to empty as I am expecting. Any help on this would be greatly appreciated. I am new to JS.

<script src="https://code.jquery.com/jquery-latest.min.js"></script><script>

function HideFields() {
    //Enter the fields you would like to hide here.
    fieldsToHide = fldList;

    //Get all SharePoint fields
    var formFieldTitles = $(".ms-formtable td.ms-formlabel h3.ms-standardheader");

//Iterate over each row in the form
formFieldTitles.each(function () {

    //Get the text of the field title
    var textToMatch = $(this).text();

    //Get the table row associated with this title
    var currentRow = $(this).closest('tr');

    //Iterate over our list of fields we wish to hide
    for (var i = 0; i < fieldsToHide.length; i++){
        var field = fieldsToHide[i];

        //Match the SharePoint field name to our field name
        if (textToMatch.toLowerCase().replace("*", "").trim() === field.toLowerCase().replace("*", "").trim()){

            //Hide this field
            $(currentRow).hide();    
            }
        }
    });
}

function AddToBodyOnLoad(){
    //Ensure that our function is called last by pushing it again
    _spBodyOnLoadFunctionNames.push("HideFields");
}

$(document).ready(function () {
    var value = $("select[title='Activity Type Required Field'] option:selected").text();

    if (value = 'New'){
        fldList = ["Additional Information Required from Applicant", "Assigned To (Field)", "Date Lands Officer received", "Date Lands Officer started merit review", "External Referral Required", "External Reviewer", "Inspection", "Internal Referral Required", "Internal Reviewer", "Merit Recommendation by Field", "Merit Upload to ECM complete", "Referral Due Date", "Zone", "FNC", "Merit Decision Letter", "Review Merit Recommendation by PAS", "Security"];
    }
    else if (value = 'Renewal'){
        fldList = [];
    }
    else{
        fldList = [];
    }

});

//Add our function to the array of onload functions
_spBodyOnLoadFunctionNames.push("AddToBodyOnLoad");</script>
1

There are 1 best solutions below

0
On

If you're trying to hide field(tr) by filed title, you may try to use $('nobr:contains("field title")'), anyway, for JavaScript/jQuery script, it's helpful to debug by developer tool(F12) always.

$('nobr:contains("field title")').closest('tr').hide();

Here is one similar thread with my sample code in SharePoint 2013.

https://social.technet.microsoft.com/Forums/office/en-US/d14766f1-d085-48c3-9efc-16e46ca64bc5/hide-a-field-based-on-the-value-from-another-field?forum=sharepointgeneral