May Y
May Y

Reputation: 179

AJAX success not render properly since second request

I use ajax to render output to below HTML element.

  <p class="result-box" id="result-box">The result is : <br><strong id="result"></strong></p>

Everything works fine when rendering result for the 1st input request. When I update my input, the console changes and prints desired data but the webpage including the text does not change.

I get Cannot read property 'setAttribute' of null at canvas_and_ploton the 2nd+ time refresh below, If I remove setAttribute, I get Cannot read property 'getAttribute' of null at canvas_and_plot.

$(document).on('submit','#cat_select',function(e){
    e.preventDefault();
    $.ajax({
        type:'POST',
        url:'/cat_select',
        data:{
            l2:$('#l2').val(),
            l3:$('#l3').val(),
            csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
        },
        success: function server_response(response) {
                const r = JSON.parse(response);
                console.log(r); //updated
                var cat_result = r.cat_result;
                console.log(cat_result[0]) //updated
                var text=$('<i></i>');
                $.each(cat_result, function(idx, value) { 
                text.append('<id="result'+idx+'">' + cat_result[idx][0]+ '<br>'); //even the text output are not updated
                text.append('<canvas id="canv_'+idx+'" width="200" height="200"></canvas><br>');
               });
               $('#result').replaceWith(text);
               $.each(cat_result, function(idx, value) { 
                canvas_and_plot(idx,cat_result[idx][9],cat_result[idx][10],cat_result[idx][11])});

}
function canvas_and_plot(idx,a,b,c) {
var canv = document.getElementById('canv_'+idx);
canv.setAttribute('id', 'canv_'+idx);
var C = document.getElementById(canv.getAttribute('id'));
//plot...
}

I tried adding cache: false and adding random number to the url but neither of them works.

Why only error after the first request? How can I fix this? Thanks

Upvotes: 0

Views: 54

Answers (1)

Filipp Sher
Filipp Sher

Reputation: 128

The reason for it not changing is that you are replacing your block #result with received data.

$('#result').replaceWith(text);

After which you'll a dom tree that looks something like this:

    <p class="result-box" id="result-box">
        The result is : <br>
        <i>
            <id="result123">...<br>
            <canvas id="canv123" width="200" height="200"></canvas><br>
            <id="result321">...<br>
            <canvas id="canv321" width="200" height="200"></canvas><br>
        </i>
    </p>

So the second time you click there is no element with #result id.

I suggest (as a rough and quick solution):

On every request remove all children of your #result element and fill it again with new data. So add this line before making the request - $('#result').empty(); and replace $('#result').replaceWith(text); with $('#result').append(text);

The main idea is to keep the place you put and add your server data. Hope this helps!

Upvotes: 1

Related Questions