Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strike>As you don't know which div elements to remove, the only way is removing them all then adding those you get from the AJAX response.</p> <p>To remove them all, use the class that you already have:</p> <pre><code>$(".char").remove(); </code></pre> <p>Add this line to the <code>success</code> function, before iterating over the lines.</strike></p> <p>OK, you can save the ID values returned by the AJAX call in array then remove any that does not exist in the array. Revised function code:</p> <pre><code>success: function(data) { var str = data; var chars = str.split("&lt;br /&gt;"); var arrDivIDs = new Array(); for(var i = chars.length - 1; i &gt;= 0 ;i-- ) { chars[i] = chars[i].split('/'); arrDivIDs[chars[i][0]] = true; var o = document.getElementById(chars[i][0]); var aimt = i; if (!o) { if (aimt!=chars.length -1) { $('#gracze').html('&lt;div id="'+chars[aimt][0]+'" class="char"&gt;&lt;div id="char_name" style="left:-'+(((chars[aimt][3].length*9)/2)-16)+'px;width:'+(chars[aimt][3].length*9)+'px;"&gt;'+chars[aimt][3]+'&lt;/div&gt;&lt;/div&gt;'+$('#gracze').html()); $('#'+chars[aimt][0]).css("top", chars[aimt][2]*32-16+"px"); $('#'+chars[aimt][0]).css("left", chars[aimt][1]*32+"px"); $('#'+chars[aimt][0]).css("z-index", chars[aimt][2]+1); } } else { $('#'+chars[aimt][0]).animate({ "top": chars[aimt][2]*32-16+"px", "left": chars[aimt][1]*32+"px" }, { duration: 300}); $('#'+chars[aimt][0]).css("z-index", chars[aimt][2]); } } $(".char").each(function(index) { if (!arrDivIDs[$(this).attr("id")]) $(this).remove(); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload