<head>
<style>
.hiddenanswer { display:none;}
</style>
</head>
<body>
<div id = "allquestions">
<div class="questions">
<p> What happens if i want to buy an item </p>
<p class="hiddenanswer> this cost 600 pounds but we offer dis counts </p>
</div>
<div class="questions">
<p> What happens if i want to buy this item </p>
<p class="hiddenanswer"> this cost 400 pounds but we offer discounts </p>
</div>
<div class="questions">
<p> What happens if i want to buy a unit </p>
<p class="hiddenanswer"> this cost 50 pounds but we offer discounts </p>
</div>
</div>
<script>
var para = document.querySelectorAll(" .questions p:nth-child(1)");
para.addEventListener('click', displayAnswer);
function displayAnswer () {
this.parentNode.lastElementChild.style.display = "block";
}
</script>
</body>
I am in the early learning phase of javascript and really want to come to terms with this. If my understanding is correct , this should work but the browser is not recognising querySelectorAll. I want to achieve a FAQ page on practice project and for the answer to appear once i click on the question.Each answer has a css class which displays them as "none" and when clicking the question, they appear as "block". If i target each P element seperately via document.getElementsByClassNames and selecting my class Node and add an event listener seperately to each of the p elements , it works, but i assumed querySelectorAll should work here so that i wouldnt have to take those lengthy steps. Could it be a problem with my browser or is it a misunderstanding of querySelectorAll on my part ? Thanks all