Tengo una serie de objetos que son preguntas. Esto es para un cuestionario de preguntas aleatorias. Básicamente, cuando se selecciona una pregunta al azar, pensé que todos los atributos irían con ella (si se "llamaba" correctamente). No estoy seguro de cómo mostrar la imagen del objeto cuando aparece el texto de la pregunta (q).
const questionBank = [ { q: "This is question 1's text", options:['1','2','3','4', '5'], answer:4, imgURL: 'http://drive.google.com/uc?export=view&id=googleDriveId', }, { q: "question 2 will go here", options:['7','6','4'], answer:0, imgURL: 'http://drive.google.com/uc?export=view&id=differentDriveId', } function getNewQuestion(){ const question = document.querySelector(".question"); //set question text question.innerHTML = currentQuestion.q; //add image to question //document.querySelector('.question').appendChild(img); (attempted, did not work) //var img = new Image(); (attempted, did not work) //img = document.createElement("img"); (attempted, did not work) //img.src = document.querySelector('.imgUrl'); (attempted, did not work)Creo que ese es todo el código relevante.
Estaba en el camino correcto al crear un nuevo elemento img, si echa un vistazo a esta publicación, necesita crear un nuevo elemento de imagen.
en su última línea, en su lugar, configure image.src = currentQuestion.imgURL como desea que la propiedad imgURL de las preguntas actuales, luego puede appendChild() su nuevo elemento de imagen.