I am trying to print the response received from my AJAX call to my PHP script. This is my code:
JavaScript:
$.ajax({
url: 'index_backend.php',
type: 'POST',
data: { "input": "id"},
success: function(response) {
let str="";
const arr=response.split(" ");
for(var i=0; i<arr.length-1; i++){
str=str.concat(" ",arr[i]);
}
//console.log(arr); shows correct output
//console.log(arr[arr.length-1]); shows correct output
document.getElementById("qid").style.display="block";
$("#qid").text(str); //works
$("#qrtr_id").html(arr[arr.length-1]); //doesn't work
},
complete: function() {
}
});
HTML:
<span id="qid" style="display: none;">
Some random text here :
<span id="qrtr_id" style="color: crimson;"></span>
</span>
All I am trying to do, is split the response and print a part of the response (excluding the last word) in a separate <span> and the last word in a separate <span>. But the last word never gets printed, although when I console.log() the word, then it shows correctly.
I've re-checked my <span> ids and they are correct too. Where am I going wrong?