A simpler and cleaner way of achieving the desired output can be implemented as follows using jquery
{% for qts in page %}
<input type="button" value="{{qts.opt1}}" class="quest-btn" data-answer="{{qts.answer}}" />
{% endfor %}
<script>
$(".quest-btn").click((e) => {
const targetElement = e.target;
console.log(
targetElement.dataset.answer,
targetElement.value
);
});
</script>
In the about code quest-class is just a dummy class to attach click event listener to multiple button you can use any common class in buttons.
to know more about jquery click event listener check this out
for more info about data-attributes check this
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…