Skip to content

Instantly share code, notes, and snippets.

@vtrpldn
Last active December 20, 2019 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtrpldn/529096172a54150377d9575ec58ccfa7 to your computer and use it in GitHub Desktop.
Save vtrpldn/529096172a54150377d9575ec58ccfa7 to your computer and use it in GitHub Desktop.
const comments = document.querySelectorAll(".comment");
comments.forEach(function(element, index) {
const toggleButton = document.createElement("div");
const commentMeta = element.querySelector(".comment-meta");
const currentComment = element.querySelector(".comment-content");
const commentAuthor = element.querySelector(".comment-author");
const commentMetadata = element.querySelector(".comment-metadata");
const commentReply = element.querySelector(".reply");
const childComments = element.querySelector(".children");
commentMeta.append(toggleButton);
commentMeta.style.position = "relative";
toggleButton.innerHTML = "[-]";
toggleButton.style.cssText =
"position: absolute; right: 0; top: 50%; transform: translateY(-50%); cursor: pointer;";
toggleButton.addEventListener("click", function(event) {
if (currentComment.style.display === "none") {
toggleButton.innerHTML = "[-]";
currentComment.style.display = "block";
commentReply.style.display = "block";
commentMeta.style.marginBottom = "1em";
commentAuthor.style.opacity = "1";
commentMetadata.style.opacity = "1";
} else {
toggleButton.innerHTML = "[+]";
currentComment.style.display = "none";
commentReply.style.display = "none";
commentMeta.style.marginBottom = "-20px";
commentAuthor.style.opacity = "0.3";
commentMetadata.style.opacity = "0.3";
}
if (childComments) {
if (childComments.style.display === "none") {
childComments.style.display = "block";
} else {
childComments.style.display = "none";
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment