Reputation: 6887
var returnResponse = function (click, toElement, getParser) {
$("body").on('change', click, function (event) {
$(toElement).empty();
removeExistingDataFull();
$.ajax({
url: "../Controller/jsonParser.php",
type: "GET",
data: getParser + '=' + $(click).val(),
dataType: 'JSON',
success: function (response) {
$(response).each(function () {
console.log(this.getParser);
$(toElement).append($("<option>").attr('value', this.getParser).text(this.getParser));
});
}
});
});
};
returnResponse('#CouserFinder', '#RegType', 'CT_Course_Code');
Here is the console.log(this);
output
But when i used console.log(this.getParser);
,It show me undefined
Here is the console log output
What i'm doing wrong ?
Upvotes: 0
Views: 69
Reputation: 1074248
I think you're looking for bracketed notation:
$(toElement).append($("<option>").attr('value', this[getParser]).text(this[getParser]));
// -------------------------------------------------^---------^-----------^---------^
In JavaScript, you can access a property in one of two ways:
Using literal ("dotted") syntax, foo.bar
, and
Using string ("bracketed") syntax, foo["bar"]
In the second case, the string can be the result of any expression, including a variable lookup. So this[getParser]
will look up whatever property is named by the string in getParser
. If getParser
is "CT_Course_Code"
, then this[getParser]
will look up the CT_Course_Code
on this
.
Side note: Your code is looking for CT_Course_Code
property, but your screenshot suggests that the objects in the response
array don't have a CT_Course_Code
property. They do have CT_Type_Code
, though. That's assuming response
is an array, it's hard to tell as you haven't shown us the actual JSON being returned.
Upvotes: 3
Reputation: 22711
Can you try this,
$.each(response, function( index, value ) {
console.log( index + ": " + value );
$(toElement).append($("<option>").attr('value', value ).text(value ));
});
Upvotes: 0