2
\$\begingroup\$

I converted an old Wordpress Plugin of mine from jQuery to plain JavaScript using a cheat sheet. I was very surprised that it seems to work right out of the box on my first try.

(function ($) {
    $(document).ready(function ($) {

        $('.comment-list > li .comment-reply').each(function () {

            var reply = this;

            /* collect every comment which needs a reply link */
            var allchildren = $(reply).parent().children('.children').children().find(".comment-content");

            $.each(allchildren, function (index, value) {
                $(reply).clone().appendTo(value);
            });

        });
    });
})(jQuery);

Became this:

const replybtns = document.querySelectorAll(".comment-list > li .comment-reply");

for (let i = 0; i < replybtns.length; i++) {
    let replybtn = replybtns[i];
    let replybtncp = replybtn.cloneNode(true);
    let cc = replybtn.parentElement.querySelectorAll('.children div.comment-content');
    let lastcc = cc[Object.keys(cc)[Object.keys(cc).length - 1]];
    lastcc.appendChild(replybtncp);
}

This copies all reply links of all level 1 nested comments and appends them to the last comment.

Is this code okay? Is it really that simple? I tested it with Firefox and Chrome, and it seems to work. I know that it appends the reply link only to the last element. I did that on purpose to make it simpler.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review! Your post looks good, hope you get some good answers! \$\endgroup\$
    – ferada
    Commented Mar 15, 2021 at 23:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.