// on DOM load, run this function
$(function() {
var commentList, comments, replies, controls;
// NECESSARY INITIALIZATION
// get the comment container
commentList = $('.commentList');
// get all comments
comments = $(commentList).find('.comment');
// get all replies (all comments with a data-reply-to attribute)
replies = $(comments).filter('[data-reply-to]');
// nest them
nestComments();
// CONTROLS (wouldn't be present in actual implementation)
// create the controls to nest and un-nest at the top of the body
controls = $('<div class="controls"><a href="#">nested</a> | <a href="#">single nested</a> | <a href="#">linear</a></div>').prependTo(document.body);
// add click events to the controls
$(controls).find('a').eq(0).click(prevent).click(nestComments);
$(controls).find('a').eq(1).click(prevent).click(singleNestComments);
$(controls).find('a').eq(2).click(prevent).click(unNestComments);
// function: prevent default action from happening on an event (e.g. following a link)
function prevent(e) {
e.preventDefault();
return false;
}
// THE (UN)NESTING FUNCTIONS
// function: take the nested comment list and linearize them
// this function relies on the original order being correct
function unNestComments() {
// get all comments, and attach them to the commentlist container
comments.appendTo(commentList);
}
// function: take the linear comment list and nest them
function nestComments() {
// get all elements with a "data-reply-to" attribute, and detach them
// then loop through the detached elements, applying this function
replies.each(function(i, el) {
var targetId;
// wrap the element in a jQuery collection
el = $(el);
// get the data attribute
targetId =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.