Simple Threaded Comments with jCollapsible (new jQuery Plugin)

jCollapisble is a JQuery plugin that takes any nested list (OL or UL that have children) and coverts it into collapsible threads. This is especially useful to create simple threaded comments on a blog or forum without having to modify the backend code. I’ve tried to keep the plugin as simple and bare-bones as possible, so right now there are no fancy slide or fade effects. The only thing required is the plugin JS file itself – CSS and images are completely optional.

The plugin has been tested (and works fine) on Firefox 3, Chrome 5, IE7+, Safari 4 and Opera 10. It should work on older browsers too though the CSS may need to be fiddled with.

Example

This page actually has the plugin enabled – you can see it in action on the following list or in the comments section!

  • Test Parent
    • Test Child
    • Test Child 2
  • Test Parent 2 (childless)
  • Test Parent 3
    • Test Child
      • Child Level 3
    • Test Child 2

Download

You can download the full source, minified version or view the example online.

jCollapsible Plugin (zip)View Demo

Usage Instructions

SliderNav is super easy to use, but first you have to load JQuery, the plugin and the relevant CSS file in order to use it:

1
2
3
4
5
6
7
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="jCollapsible.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
    $('#example').collapsible({xoffset:'-10',yoffset:'5',defaulthide: false});
});
</script>

Simple enough – just include JQuery (1.3.2 and up), the jCollapsible file and then call the function with your settings.

Customize the Icon or Use Images Instead

While I did try to keep the plugin lightweight, I also wanted to make it somewhat extensible by adding a few different options, including:

  • defaulthide – hide all children by default (default: true)
  • symbolhide – symbol to hide child elements (default: -)
  • symbolshow – symbol to show child elements (default: +)
  • imagehide – image to hide child elements (default: null)
  • imageshow – image to show child elements (default: null)
  • xoffset – amount pixels to move symbol/image horizontally (default: -15)
  • yoffset – amount pixels to move symbol/image vertically (default: 0)

Please note that the image values are null by default (using symbol instead) and the offset values are there to fine-tune the location of the image/symbol.

The Code

For those who are curious, here is the source for the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$.fn.collapsible = function(options) {
    var defaults = {defaulthide: true, symbolhide: '-', symbolshow: '+', imagehide: null, imageshow: null, xoffset: '-15', yoffset: '0'};
    var opts = $.extend(defaults, options); var o = $.meta ? $.extend({}, opts, $$.data()) : opts; var obj = $(this);
    if(o.imageshow) o.symbolshow = '<img src="'+o.imageshow+'" class="jc-show" border="0">';
    if(o.imagehide) o.symbolhide = '<img src="'+o.imagehide+'" class="jc-hide" border="0">';
    var startsymbol = o.symbolshow;
    $('li', obj).each(function(index) {
        if($('>ul, >ol',this).size() > 0){
            if(o.defaulthide) $('>ul, >ol',this).hide(); else startsymbol = o.symbolhide;
            $(this).prepend('<a href="" class="jcollapsible" style="position:absolute;left:'+o.xoffset+'px;top:'+o.yoffset+'px;">'+startsymbol+'</a>').css('position','relative');
        }
    });
    $('.jcollapsible', obj).click(function(){
        var parent = $(this).parent();
        $('>ul, >ol',parent).toggle();
        $(this).html($(this).html() == o.symbolshow ? o.symbolhide : o.symbolshow);
        return false;
    });
};

I wasn’t exaggerating when I said this plugin was simple! It first few lines initialize the plugin/function and set the default values. The each function creates the actual expand/collapse icons and the last function (click) just toggles the child elements.

Final Thoughts

This plugin actually comes from a function I wrote for one of my personal projects, I just thought some of you guys may find it useful so I’ve wrapped it up as a plugin. If you do use it (or like it), please leave some feedback, good or bad – it’s highly appreciated and helps me produce better work.

As always, subscribe to the feed if you haven’t and follow me on Twitter!

  • http://devgrow.com/ Monji

    Testing threaded comments

    • http://devgrow.com/ Monji

      This is a threaded reply!

      • http://devgrow.com/ Monji

        Seems to be working so far.

      • Dude

        test reply! does it fade in smoothly?

      • Dude

        no! A reply does not update the tree, but the whole site is reloaded.
        What a pity!

      • http://devgrow.com/ Monji

        The plugin only adds the collapsible part, to have comments be posted without refreshing you would need to use AJAX – something not implemented in this blog. If you want, you can easily have AJAX comments using one of the many plugins available.

      • http://pero.com pero

        tst123

      • http://rr 345t3

        rrertert

      • Mario

        This is very cool :)

      • http://test test test

        testtest

      • seb

        testing the plugin

      • Ant

        This is a nice, clean plugin :) good work

      • Ant

        Just testing…

      • http://wanderfulworld.com Ray

        I see trees of green

      • http://gyanmanthan nitin

        testing

      • http://sdaf johan

        testing

      • blah blah

        testing!!!

      • jona

        Where is the part of the html ?

      • chengye

        testign again

      • abhi

        good work monji

      • mark

        mark test

      • http://www.csinfotech.com sdsd

        aaaaaaaaaaaaaaaaaaa

      • http://ddd ddd

        dddd

      • Mark

        impressive plugin

      • Tova

        test

      • Tova

        test22

      • Tova

        test333

      • Tova

        test sdf sdf

      • Tova

        level test

    • http://devgrow.com/ Monji

      Last test, then changing the icon to an image and defaulthide to false!

      • http://super TESt

        spuoer

      • uaretroublesome

        testing

      • uaretroublesome

        nest the nest in the nest before the nest gets nested.

      • http://www.pennyauctionstar.com stranger

        Just test

      • asdf

        testttttt

      • Tova

        Teasdfasdf

    • kato

      i guess this is ok but isnt for me

      • http://testing testtest

        testing threaded comments

      • http://Test.com JoeTest

        teste

    • boom

      testing nested comments

      • Testing

        Testing. I think it’s pretty cool.

    • http://www.lowgen.co.cc dotkop

      hahaha… cool… but is this trick work for blogger?

    • test

      TEST!

    • http://www.csinfotech.com sdsd

      testttttttttttttt

      • test

        Another testttttttttttttt

      • test

        another same level testttttttttttttt

    • http://Test.com JoeTest

      JoeTest

    • http://asdasd hello

      test

      • test

        another hello

    • test

      another test

      • test

        another another test

    • asdfasdf

      test

  • http://www.nathanielks.com Nathaniel

    Dude, the only thing it’s missing is animation. If they could slide in and out when they are collapsed/expanded, it would be the perfect plugin!

    • http://devgrow.com/ Monji

      Since this is pretty trivial, I’ve updated the plugin to include a slide down/up function. If you want to use a different effect, just modify line 21 in the plugin file to reflect whatever it is you want to achieve.

      Hope you like it, and thanks for the feedback!

      • Dude

        Just a test.

      • joe

        another test reply

      • boom

        im just testing again

  • http://twitter.com/eduardobona Eduardo Bona

    For best styling of the effect I would give a hint in the css link with: a{outline:0}

    • http://devgrow.com/ Monji

      Thanks for the suggestion Eduardo, I’ve added that style to the element – feel free to download the plugin or zip again, or simply add that bit (or your own styles) to line 16.

  • http://apperceive.com Markus Sandy

    Nice. One small thing: the title tag on the demo page is from a previous article. You might want to fix that.

    • http://devgrow.com/ Monji

      Thank you for pointing that out Markus, I’ve corrected it.

  • Pingback: Utiliser JCollapsible pour faire un arbre | Tempo Bamako

  • mesut

    test comment

  • http://store.apple.com/ie Rich

    Test comment

    • Randy

      Cool, I cannot wait to give this a try tonight. Thanks for publishing.

      • sasa

        thanks

      • homer

        testing a reply

      • http://blah.com Bob

        Test

  • http://pero.com pero

    tst123

    • eee

      wwww

  • http://pero.com pero

    tst123

  • http://www.tam.com aaaa

    test me 123

    • Pablo

      It seems to be great

      • Amit

        sasdas

        asd

        asd
        as
        d
        as
        dasd
        asd

        ad
        as
        d
        as
        d
        asd
        a
        ds
        asd

      • mark

        testing comments

      • http://test Romulo

        just testing

  • http://www.softlinetek.com/ WebSite Design Compmpany

    such a huge collection of design layout tutorials…. almost overwhelming me…. but it is very fantastic too…. looking through these all very slowly…. thanks again

  • Amit

    asdasd

    asd

    asd
    as
    d
    asd
    as
    d
    asd

    asd
    as
    d
    as
    da
    sd
    asd

    asd
    asd

    • Amit

      Is this the new me, replying to the old me ??

      Hell Yeah !!

      Cool Plugin …. Found it again after years :)

  • http://findnotice.com Brad

    This is great work!

  • http://swissarmysoldierknife.com jakup

    Thank you for this plugin

  • Mauricio Fragale

    Olá!!

  • http://www.datawit.it Michele Ziparo

    I’m testing this plugin … it’s very good ;)

  • Tom

    Nice work

  • http://www.bravo-kernel.com bravo_kernel

    Great work, your plugin ran against my huge generated tree instantly without any problems.

    There is one issue in IE7 though. Collapsing and then expanding a node will make the underlying nodes invisible. The html is there, it’s just not visible. Funny enough you can reproduce the issue by viewing this using IE7 if you wan’t to see it for yourself.

    Otherwise an awesome plugin!

    • http://mysite.com Hello

      Testing your comment system…

      • Not a Name

        Hello thi s is not a test

  • ハイロ

    Interesting thing, I will play around with it~
    test~

  • andrew

    Hey, Great work! Is there anyway to add a Expand and Collapse All funtion?

    Thanks

  • DfwDude

    jjust a test

  • http://tes.com ted

    jjust another test

  • Zeke

    Adding a comment…

  • http://sodidi.ramjeeganti.com Ramjee

    Hi Monji,

    Just what I was looking for.

  • Checking this out !

    :D

  • Sebastian

    Testing comments, btw, very nice plugin.

    • eew

      test

      • sr

        testing.

  • http://nowebsite Roxanne

    Hi there, just became aware of your blog through Google, and found that it’s truly informative. I am gonna watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers! my website is about truss bridges. would like some feedback if possible

  • ardsa

    das

  • http://blog.kingpepe.de Peter

    Hi Monji,
    very cool Plugin, thank you!
    Is there a way to not only expand the list by clicking the arrow but also the word in the list?

  • abhi

    gonna try this

    • abhi

      hmm.. shall try it 2morow again

  • tomtom

    Excellente

  • http://link Pqckuwjl

    rhinestone crowns,

  • tony

    test away

  • chandana

    testing this

  • http://hotmail.com Test123

    Test Test

  • لا إله إلا الله

    لا إله إلا الله محمد رسول الله

  • Carla

    It would be interesting if you can post some hints about actually making the comment page.
    Any help is very much appreciated.

  • http://www.ziki.com/en/jamar-dominguez-13+244499/post/Article-dashboard.com-will-be-Trusted-Through-Thousands-and-thousands-because-the-Origin-Intended-for-Excellent+12791569 Burton Miramon

    I think other web site proprietors should take this site as an model, very clean and excellent user friendly style and design, let alone the content. You’re an expert in this topic!

  • http://shsh xyz

    TESTING

  • Gaurav

    it seems to work correctly with IE but crashes with firefox 6.0.1 …..does any1 knw what may be causing this

    thanks

    • JaKay

      I see that IE has one problem, the arrow-right and arrow down don’t always show as they should when collapsing comments…

      • JaKay

        meant to say re-collapsing…

  • Pingback: Fresh and Latest jQuery Write Less Do More Episode-1

  • http://none hallo

    Hallo
    test

    • hi

      test1

      • serg

        12345678

      • http://test test

        test

  • http://www.dddd.com sss

    aggd ghghadg hg hgad hg hgda

    • http://www.dddd.com sss

      cxcxc

    • cool

      what?

    • cool

      news to me

  • http://apple.com Hermit

    interesting

  • Pepe

    YEHHH

    • rtrtrtfd

      rtrtrtrtdfdf

  • http://test wowow

    test replay too

  • http://www.degraeve.com/images/beach.jpg Author22222

    bfhdfhhddhf

  • http://AmericanSolarEconomy.blogspot.com asdf

    Test here too.

    • http://AmericanSolarEconomy.blogspot.com asdf

      Another one.

      • http://AmericanSolarEconomy.blogspot.com asdf

        One more.

    • cxzczx

      xczcxzvxczvcvcxvcxvx

  • dave

    test

  • http://www.maxbilisim.com Maxbilişim

    Güzel bir çalışma

  • test

    Teeeeest

  • test

    dd

  • test

    my test

  • David

    Hey, this is a very nice piece of work. Could you please post your backend code and table design? Without it, there is no way to reproduce your work. For threaded discussions, actually, the backend code/design is also the key to the whole solution.

    Best.

  • http://primacoustic.com Dave XXX

    This is a test XXX

  • df

    gf

    • Amenomkar

      awesome……//

  • chris

    lol

  • xccx

    ddsdsd

  • http://rrrrr.com sfsdf

    ghjghfgjfgjghj

    • asdf

      testtttttttttttttttt

      • Chose1374

        3 lvl

  • http://www.444.com John Doe

    Test by john doe

  • Chose1374

    Test.

    • Abc

      sdgdfgsdfgdsfgsdfgsdgsdfg

  • Asdr

    dsdfsdfsdfsdf

    • Dasrer

      fsdfsdf

      • Samanthamerley

         My reply

      • Test

        sssad sda

      • Qwq

        qweqeqw

      • Qwq

        qweqweqweqweqw

      • Qwq

        qweqweqweqweqwe

      • Qwq

        55555

      • Jhh

         mn mn 

      • pras

        test

      • asdas

        sdfdsfs

    • Test

      test

    • Gg

        bbbnbn

  • MyTestName

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    • MeMe

      Hi!!

    • Hgfhg

      hey you

    • Hgfhg

      test 

      • sss

        hi

      • Tova

        test

  • Pingback: 25 Cool and Helpful jQuery Plugins/Tutorials For Your Next Project | 1webdesigner.net

  • Gillanrose

    fsdfsdf

    • Asdasdasd

       hhhhhhhhhhhhhhhhhhhh

  • Qwq

    what??

    • Qwq

      asdasdasd

      • Qwq

        sssssssssss

  • Gse

    tes

    • Abc

      test

  • petti

    there is a prob in jcollapsible … Image is not changing but the collapse expand functionality is working.. is there a fix for this?

    • Michael Murray

      interesting.

      • Michael Murray

        this is supposed to be a reply to my reply… but it doesn’t seem to work that way.

      • Michael Murray

        actually now it is working properly.

      • Guest

        hey i was here.

      • http://www.facebook.com/VavilenTatarskiy Vavilen Tatarskiy

        Just a test.

      • http://www.facebook.com/VavilenTatarskiy Vavilen Tatarskiy

        reply on test

      • http://www.facebook.com/mastermindankur Ankur Khera

        ok

      • Tova

        Comment on ok

      • Tova

        Comment on my last comment

      • http://www.facebook.com/mastermindankur Ankur Khera

        kjkllkjkjkjkl

      • gagan

        @f961bfed580387ce1491f01aaf87c982:disqus @google-8ae26dee1b533ce98dd468004e4b39b0:disqus

      • Tova

        Comment on Guest comment

      • Kjsbjws

        comment here

    • Cissurf

       yes .good

    • Trancer_chat

       test

      • Trancer_chat

         test

    • Trancer_chat

       fbfb

    • Test

      test

    • Dfbgn

      what ?

      • Tova

        comment on what

  • mohsen

    thanks so much

  • Sdgg

    ای کیرم تو دهن کافرتون بره کیری ها

    • Pooya Roohi

      باید هر گوری میرید وحشی بودنتون رو به اثبات برسونید؟ ملت خودشیفته عقده ای

  • Michael Murray

    and another test!

  • Dfdff

    test

  • Deepster

    jhdjkhkdjshjkdsfhfsdjkl

  • petti

    i have fixed that in with simple javascript… but i hav another problem total UI is completely different in IE8, IE6, IE7 how to fix tht need help urgently

  • Cissurf

    jjj

  • Jholhl

    ggkhgklj

  • http://www.facebook.com/VavilenTatarskiy Vavilen Tatarskiy

    Test reply on test

  • http://www.facebook.com/VavilenTatarskiy Vavilen Tatarskiy

    test reply on test

  • http://www.facebook.com/VavilenTatarskiy Vavilen Tatarskiy

    gfbgfb

  • It Hoan

    gfdgss

  • Ni

    best…

  • Alan

    helo

  • fdgfdsgsdfg

    sdfgsdgsfdg

    • 1213

       gfjfg

      • trst

        test123

      • Black dog

        Test

      • dsada

        dsadsaa

      • asdasdsa

        asdsadsa

      • dsa

        dasdad

      • aklsjda

        hi there

      • abcd

        ggffgfgfg

      • pras

        haha
        asd

      • ff

        dd

      • h

        ghjjjjjjjjjjjjj

    • Nishitmodi2591

      fhj h dtyu jdty udfyu yd ty

    • Dvdsvsd

      vdsvsdvsdvds

      • Test

         Test

    • asdasda

      asdasda

    • guest

      test

      test

      test
      test
      test

  • guest

    test

    • randy roundy

      test :)

  • Asdf

    asdfasdf

  • Asdf

    asdfasdf asdf asdf asdf asdf asdf asdf

  • omotoed

    i like this feature and will definitely include it in my website’s article page

  • X Xamiir

    test

  • Hg

    Test

  • Dfg

    sample comment

  • Dfg

    i am cool

  • Sdfds

    hello just testing

  • Nicthecatburglar

    Where does it save to?

  • erik

    testing

    • sdfsd

      hello 2

  • erik

    hello

  • david

    haaaa

  • a

    hi

  • http://www.facebook.com/mastermindankur Ankur Khera

    ok

  • jzee

    Nice plugin. However I’ve run into a problem. If I update the comment section, add more comments and re-init collapsible, only the one that’s been just added works (collapses). Previous ones just open and close.

    • jzee

      nvm. Just had to remove the ‘a’ elements.

  • Rolex replica

    As has been said often, the very first impression may be the final impression and accordingly, dress for the occasion and recall even minor specifics could make every one of the difference.

  • Breitling replica

    Breitling replica

  • jean roberto

    c’est bio

  • Guillermo Lara

    Comentarios de prueba

    • Tova

      Test

  • Federico

    m testx

  • Tova
  • mike

    My first test comment

    • mike

      my reply

  • Ashutosh

    Hello

    • Ashutosh

      Another loop

    • Michael Diamant

      Hi Ashutosh!

  • Isabelle

    Very nice!

  • test

    test

  • Pingback: CommentBox for SharePoint « SharePoint JavaScripts

  • Nikhil

    this i snice Plugin i liked it thanks

  • asdf

    asdfasdfasdfasdfasdfasdfasdf

    • fdas

      fdsafdsafdsa

  • carlo

    asaasa

  • da

    dasda

    • test

      testtes t test test

  • test

    test

    • test

      test
      testasdadadadda

  • test

    da

  • aaaa

    aaaaaa

    • dfdsfsadfd

      kjdslkjfsaklf jasfdfasdfdsdfds

  • jesus christ

    hello there

  • asd

    fhfghfghfgh

    • ewewewe

      gfhfghfg

  • bob
  • Boris

    hey

  • http://www.facebook.com/slagalica.skocko.7923 SLagalica Skocko

    odlicno

  • testy mctesterson

    test

  • dfgsdgadgad

    kmmkmkkk

  • guest

    test

    • myname

      Reply Message

    • myname

      test
      test
      test
      test
      test
      test
      test

  • czcx

    xzXZ

  • Carlos Jose Salas

    What the fuck!!

  • http://www.facebook.com/salas.charlie Carlos Jose Salas

    Esto es una prueba, solo una prueba!!!

  • Guest

    test

  • hhh

    kjkjkjjj

  • aaa

    aaa

  • awdwadawda

    adawdadaw

  • kdfo

    kxcvkxcv

    • kdfo

      difsfl

  • Hamza Zafar

    kdsfmsdkf

    • Hamza Zafar

      koxcd

      • Hamza Zafar

        kmedkfsmdl

      • Hamza Zafar

        ;sdfsdfsdf

    • Hamza Zafar

      dfvds

  • Hamza Zafar
  • Hamza Zafar
  • Hamza Zafar

    fgdfg

    • Hamza Zafar

      kdfmvkfd

      • Hamza Zafar

        dfmvdk

  • Hamza Zafar

    jusdfosdjf

  • Hamza Zafar

    odfv]