I’ve got an array of comments, and want a part of them to be hidden, if hidden
param is true
, before a user clicks on a "load more" button.
But it doesn’t work properly, instead it adds hidden
class to all comments but the last one.
async function comments(...arr) {
let i = 0;
const container = document.querySelector('.comments')
function setHidden(h) {
if (h === true) {
$('.comment').addClass('hidden')
} else {
$('.comment').removeClass('hidden')
}
}
for (let i of arr) {
await setHidden(i.hidden)
$(container).append(`<div class="comment">${i.comment}</div>`)
}
}
comments({
comment: 'hi'
}, {
hidden: true,
comment: 'how are you'
}, {
hidden: true,
comment: 'where are you from?'
});
2
Answers
Firstly, as mentioned by @Andy in the comments, you don’t need async/await here, as
setHidden()
doesn’t return a promise.The reason your current logic doesn’t work for the last comment is because you haven’t added it to the DOM at the point you invoke the
setHidden()
function in the final loop iteration.Also note that the logic is flawed because all
.comment
elements will be hidden based on the state of the lasti.hidden
value. You should instead set the.hidden
class on the.comment
you create within thefor
loop, not all of them at once.It’s also a simple exercise to do this without jQuery; no point adding a library for a single line of code.
Following on from Rory’s answer, if you did want to use jQuery throughout (you’re mixing and matching with vanilla JS at the moment) you can.