I have the following code, and there's no errors, but it seems to not make any text show…
- User
I have the following code, and there's no errors, but it seems to not make any text show up, despite their being no errors. How can I fix this? (I have ajax installed on the server, and it does work, and there is text in my text files that matches correctly) function loadcontent() { document.getElementById("forum-content").innerHTML = '';
function handleSuccess(response) { console.log(response); const texts = response.split(/\n/); const textdata = response.split("~"); for(let i = 0; i<textdata.length; i++) { let length = textdata[i].split("\n").length; var divs = document.createElement("div"); for(let j = 0; j<length; j++) { console.log(texts[j]); let len = texts[j].length-2; if (texts[j].substring(len, len + 1) == ":") { const para = document.createElement("u"); para.innerHTML = ">>> " + texts[j]; divs.appendChild(para); } else { const para = document.createElement("p"); para.innerHTML = texts[j]; divs.appendChild(para); }}
} }
- Assistant
It seems that you have defined the
loadcontentfunction, but you forgot to call it. You need to add a function call to execute the function and load the content.Additionally, you are creating div elements and appending them to the DOM, but you forgot to append the divs to the
forum-contentelement. You can useappendChildto append the divs to theforum-contentelement.Here's an updated version of your code with the necessary changes:
function loadcontent() { document.getElementById("forum-content").innerHTML = ''; function handleSuccess(response) { console.log(response); const texts = response.split(/\n/); const textdata = response.split("~"); for (let i = 0; i < textdata.length; i++) { let length = textdata[i].split("\n").length; var divs = document.createElement("div"); for (let j = 0; j < length; j++) { console.log(texts[j]); let len = texts[j].length - 2; if (texts[j].substring(len, len + 1) == ":") { const para = document.createElement("u"); para.innerHTML = ">>> " + texts[j]; divs.appendChild(para); } else { const para = document.createElement("p"); para.innerHTML = texts[j]; divs.appendChild(para); } } document.getElementById("forum-content").appendChild(divs); } } // Call the handleSuccess function with the AJAX response handleSuccess("Your AJAX response goes here"); } // Call the loadcontent function to load the content loadcontent(); - User
I forgot to give you the full function, my bad. (also yes I do already call loadcontent()) function loadcontent() { document.getElementById("forum-content").innerHTML = '';
function handleSuccess(response) { console.log(response); const texts = response.split(/\n/); const textdata = response.split("~"); for(let i = 0; i<textdata.length; i++) { let length = textdata[i].split("\n").length; var divs = document.createElement("div"); for(let j = 0; j<length; j++) { console.log(texts[j]); let len = texts[j].length-2; if (texts[j].substring(len, len + 1) == ":") { const para = document.createElement("u"); para.innerHTML = ">>> " + texts[j]; divs.appendChild(para); } else { const para = document.createElement("p"); para.innerHTML = texts[j]; divs.appendChild(para); }}
} }
function handleError(xhr, status, error) { console.error(error); const para = document.createElement("h2"); para.innerHTML = error; document.getElementById("forum-content").appendChild(para); }
$.ajax({ type: 'GET', url: 'writetofile.php', success: handleSuccess, error: handleError });
}
- Assistant
Thank you for providing the additional code. In this case, it seems like you are using jQuery's AJAX (
.ajax()) method to make the request to the server and handle the responses.Since you mentioned that there are no errors and the AJAX request is working correctly, the issue may lie in the response you receive from the server. Please ensure that the response is in the expected format and contains the necessary information for populating the element.