I made this code to show the image of a character in a pop up when you click on his name. My problem is that it's showing the url from my api and not the image and it's always the same one who is showing in the pop up, not the next one.
Here is my js :
function afficherConservation(id){
let popup = document.getElementById("popup");
let content = document.getElementById("content");
let span = document.getElementsByClassName("close")[0];
popup.style.display = "block";
let paragraph = document.createElement('p');
const title = document.createTextNode('Portrait : ');
paragraph.appendChild(title);
content.insertAdjacentElement('beforeend', paragraph, content);
const sautLigne = document.createElement('br');
paragraph.appendChild(sautLigne);
content.insertAdjacentElement('beforeend', paragraph, content);
const req = new XMLHttpRequest();
req.open('GET', 'http://localhost/Fil_Rouge/api.php?id=' + portrait_id, true);
req.onreadystatechange = ()=>{
if(req.readyState === 4 && req.status === 200){
const data = JSON.parse(req.responseText);
paragraph;
if(Object.keys(data).length !== 0){
const state = document.createTextNode(data[0].portrait_url);
paragraph.appendChild(state);
content.insertAdjacentElement('beforeend', paragraph, content );
}
else{
const noValueState = document.createTextNode("Non renseigné");
paragraph.appendChild(noValueState);
content.insertAdjacentElement('beforeend', paragraph, content);
}
}
};
req.send();
// When user clicks on span, close the modal
span.onclick = ()=>{
popup.style.display = "none";
content.innerHTML='';
}
window.onclick = (event)=>{
if(event.target == popup){
popup.style.display = "none"
content.innerHTML='';
}
}
}
"id" is the id of the character "portrait_url" is the picture
Any suggestion ? What did I do wrong ?