Wednesday, 17 October 2018

Why variable is undefined inside foreach function?

I have this jQuery function were I create a table:

function propertyView(values) {

// build the table
var frame = '<fieldset id = "propertiesValueArea" style="border: solid 1px #6b6b6b;">';
var content = frame + smallHeader + '<table data-role="table" id="propertiesList"><thead><tr></tr></thead>';

$.each($(values), function () {
    var data1 = this.RealValue.FieldValue;
    var data2 = this.RealValue.Id;
    //create tables row 
    content += '<tr data-id=' + this.Id + '>';
    content += '<td style="vertical-align: inherit;text-align:center;"><label for="">' + this.FieldName + '</label></td>';
    if (this.FieldValue.indexOf(',') > -1) {
        content += '<td style="text-align:center;"><select>';
        this.FieldValue.split(',').forEach(function (item) {

            if (data1 === item) //Here data1 is undefined!!!
            {
                content += '<option selected="selected" value="">' + item + '</option>';
            }
            else {
                content += '<option value="">' + item + '</option>';
            }
        })
        content += '</select></td>';
    }
    else {
        content += '<td style="text-align:center;"><input type="text" id="propFieldName" data-id="' + this.Id + '" value="' + congiValue(this.FieldValue, this.RealValue) + '"/>';
    }
    content += '</tr>';
});
content += '</table>';
content += '</fieldset>'

return content;
}

Inside outter each function I create 2 variables: var data1 = this.RealValue.FieldValue; var data2 = this.RealValue.Id;

I try to create this variable inside inner each:

 if (data1 === item) //Here data1 is undefined!!!

But on this row I get this error:

   Uncaught ReferenceError: data1 is not defined

Any idea why data1 is undefined?



from Why variable is undefined inside foreach function?

No comments:

Post a Comment