I hope you are well;
I am having problems when dynamically creating a series of cards with n elements, since each card must have a button that sends the details of this information, I want to call a function that sent me two parameters, but it marks an error:
function enviarRepuesta(x) {
sessionStorage.currentTema = x.tema;
sessionStorage.currentPregunta = x.pregunta;
window.location.assign("verPreguntaSeguimiento.html");
}
function recibirPregunta() {
let pregunta = new Array();
$.post(
"url",
{
id_usr: sessionStorage.id_usr,
id_ent: sessionStorage.id_ent,
censo: sessionStorage.censo,
ano: "2021",
tipo: sessionStorage.tipo,
},
function (json) {
if (json.response.success) {
console.log(json.data);
console.log(json.response);
pregunta = json.data;
while (document.contains(document.getElementById('containerCards'))) {
document.getElementById('containerCards').remove();
}
pregunta.forEach((x) => {
const card = `<div class="card col-5 mx-auto" id="containerCards"><div class="card-body">
<h5 class="card-title">${x.tema}</h5>
<p class="card-text"></p>
<br>
<strong for="descripcion:">Pregunta:</strong>
<br>
<p>${x.pregunta}</p>
<div class="form-group col-2 mx-auto d-block">
<button type="button" class="btn btn-primary align-right" id="agregar"
onclick="enviarRepuesta(${x})">Ver</button>
</div>
</div>
</div>`;
const elemento = document.createElement('div');
elemento.innerHTML = card;
document.body.appendChild(elemento.firstChild);
});
} else {
console.log(json.response);
}
}
);
}
As you can see, I am creating an HTML string from a forEach and I put it inside another element with an innerHTML. The detail is that I want the button with id = "agregar" to receive two parameters that go in the variable x, which are topic and question, but it marks an Unexpected identifier type error.
What I can do?
Thanks and regards.
question from:
https://stackoverflow.com/questions/66054838/create-a-function-onclick-for-everycard-element-created 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…