The data attributes are set using jinja rendered by Django. When I try to access the data-url attrib the value is assigned to the class's URL object.
I have two issues with this code. Firstly, When I try to access data-default attribute it returns null or undefined value. Secondly, when I try to assign response from the ajax call to the this.data it is not getting assigned.
Below is my code
<select id="value" data-url="api/values" data-default="1">
</select>
$(document).ready(function(){
var value = new function(){
this.url = $("#value").attr("data-url");
this.default = $("#value").attr("data-default");
this.data = null;
this.settings = {
"async": true,
"crossDomain": true,
"url": this.url,
"method": "GET",
};
this.getData = function(){
$.ajax(this.settings).done(function(response){
this.data = response;
$.each(response, function (i, value) {
var $option = $('<option>').text(value.shift_name)
.attr('value', value.id)
.attr('start', value.shift_start_time)
.attr('end', value.shift_end_time)
.attr('shift_day', value.shift_day)
if (this.default==value.id){ //Error here this.default is always undefined
$option.attr('selected', 'selected');
}
$('#value').append($option);
});//Loop ends
});//ajax ends
}//sub function ends
} //class ends
value.getData();
});