window.addEventListener('DOMContentLoaded', function(){
var viewAnswer = document.querySelectorAll('.viewAnswer'),
answer_comment = document.querySelectorAll('.answer-comment'),
close = document.querySelectorAll('.closeAnswer');
viewAnswer.addEventListener('click', function(){
answer_comment.style.display = 'block';
viewAnswer.style.display = 'none';
close.style.display = 'block';
})
close.addEventListener('click', function(){
answer_comment.style.display = 'none';
viewAnswer.style.display = 'block';
close.style.display = 'none';
})
})
for (let i = 0; i < answer_comment.length; i++) {
viewAnswer[i].addEventListener.addEventListener('click', function(){
answer_comment[i].style.display = 'block';
this.style.display = 'none';
close[i].style.display = 'block';
})
}
<div class="answer-block">
<button class="viewAnswer"> </button>
<button class="closeAnswer"></button>
<div class="answer-comment"> .... </div>
</div>
const answers = document.querySelectorAll('.answer-block');
answers.forEach(answer => {
const open = answer.querySelector('.viewAnswer'),
const close = answer.querySelector('.closeAnswer');
const comment = answer.querySelector('.answer-comment'),
open.addEventListener('click', function(){
comment.style.display = 'block';
open.style.display = 'none';
close.style.display = 'block';
});
close.addEventListener('click', function(){
comment.style.display = 'none';
open.style.display = 'block';
close.style.display = 'none';
});
});