0

I am dynamically creating elements inside a loop

m += '<input type="text" id="deadline_input_' + data[i].data.sequence + '" value="' + (data[i].deadline == "-" ? '' : data[i].deadline) + '" data-seq="' + data[i].data.sequence + '" class="hasDatepicker deadline_input" dateFormat="DD-MM-YYYY" style="display:none;" />';

m += '<span id="deadline_text_' + data[i].data.sequence + '">' + data[i].deadline + '</span>';

$('#results_projects').append(m);

but then when I use

$("#deadline_input_" + $(this).data("seq")).show();
$("#deadline_text_" + $(this).data("seq")).hide();

inside an ajax call, they are not changing. I know the ajax call is succeeding because I have added a console.log("success") inside my success which shows in the console

my full ajax call is:

$.ajax({
                    url : '/section/projects',
                    type: "GET",
                    data: {
                        "action": "update_deadline",
                        "sequence": $(this).data("seq"),
                        "deadline": $(this).val(),
                    },
                    success: function(data) {
                        console.log("success");
                        $("#deadline_input_" + $(this).data("seq")).hide();
                        $("#deadline_text_" + $(this).data("seq")).show();
                    },
                    error: function(err) {
                        alert("There was an error updateing the Project");
                    }
                });
j08691
  • 204,283
  • 31
  • 260
  • 272
charlie
  • 415
  • 4
  • 35
  • 83
  • 2
    Does console logging `$(this).data("seq")` show the proper value? – j08691 Jan 30 '20 at 15:42
  • 1
    ^^^ Keep in mind the value of `this` changes within the success callback `function(){}`, like most `this` do. You can put `context: this` on the ajax properties to force the context to remain the same in the success callback – Taplar Jan 30 '20 at 15:44

0 Answers0