2

I am using the following jquery code to get data from database-

$.get('loadTaskDetails' + $taskId, {taskId: $taskId}, function(data) {
                    $.each(data, function(i, data) {$('#ddl_Deliverables').find('option[value=data.tasks_deliverable_id]').attr('selected', true);    
                });
                }, 'json');

I have a a dropdown (ddl_Deliverables) which already contains many options. I want the option value to be set selected with the macthing value coming from database through $.get method.

Nikunj Kumar
  • 306
  • 6
  • 18

2 Answers2

2

This doesn't interpret the variable therein:

find('option[value=data.tasks_deliverable_id]')

That string value is a literal string. It's specifically looking for a value called "data.tasks_deliverable_id". To use the data variable, you need it to be outside the string:

find('option[value=' + data.tasks_deliverable_id + ']')
David
  • 208,112
  • 36
  • 198
  • 279
0
$.get('loadTaskDetails' + $taskId, {taskId: $taskId}, function(data) {
     $.each(data, function(i, data) {
        $('#ddl_Deliverables option').removeAttr('selected');
        $('#ddl_Deliverables').find('option[value="' + data.tasks_deliverable_id + '"]').attr('selected', 'selected');    
     });
}, 'json');
Borza Adrian
  • 509
  • 2
  • 12