Click here to Skip to main content
11,920,156 members (58,896 online)
Click here to Skip to main content
Add your own
alternative version


113 bookmarked

Easy DHTML treeview

, 21 Feb 2002
Rate this:
Please Sign up or sign in to vote.
A relatively easy implementation of a treeview using DHTML (Client Side Javascript in conjunction with DOM). This implementation is straightforward and doesn't require the filling of java-arrays.
<!-- Download Links --> <!-- Article image -->

Sample Image - DHTML_Treeview.jpg

<!-- Add the rest of your HTML here -->


Have you ever tried to insert an easy-to-use treeview into your homepage or even use a treeview in your web-application? If you have, you will probably know that most ready-made treeviews out there are built in a quite complex manner, making heavy use of client side javascript and difficult-to-handle structures like multi-dimensional arrays and such. There is, however, a more elegant way of implementing a treeview using only one small client-side javascript and making use of DOM (Document Object Model) and basic HTML-tags such as DIV.

Easy DHTML Treeview

The 'Easy DHTML Treeview' builds a treeview on top of the Document Object Model, which already is hierarchical by definition. The whole idea of the 'Easy DHTML Treeview' is using simple HTML tags such as DIV and A to create the real tree and then using one simple javascript function (triggered by clicking on a branch) to collapse and unfold all of the children (branches and leaves) by hiding or showing the DIV-item (if a DIV item is hidden, all the children of the DIV item are hidden as well).

Overview of design

So, when you click on the yellow 'branch name', the javascript function Toggle will be called with a pointer to the branch name itsself. The Toggle-function will then ask the document (using DOM) what the next item is (which in this case is the yellow DIV-section) and hide it. If the yellow DIV-section is hidden all items inside the yellow square (which is almost everything in the picture) will be hidden as well.

Serialising the treeview

The good thing about this simple implementation is that it is very easy to construct a treeview from data that can be stored in a database. Whereas other treeview functions and components force you to fill a javascript array (which is only 2 levels deep anyway and therefore not a real n-level tree), 'Easy DHTML Treeview' allows you to simply print the data as if it were a table. The whole functionality of collapsing and unfolding the branches in the tree is then added almost automatically by the single function that does it all!

The included demo-project shows you that it is really quite easy to create very dynamic, n-level truly recursive trees using 'Easy DHTML Treeview'. For storing the data in the tree, this data model is used:

E-R Diagram

Like a treeview, the data model itself is recursive, because topic and topic_1 are the very same table. This means topic has a relation to topic, with the subtopic table in between to implement an n-n relationship.

Tips & Tricks

I got a lot of questions about the treeview so I'd just answer some of them here for your convenience:

How can I change the initial state of the treeview from collapsed to expanded? The answer is quite simple. If you're using the static version (from the live demo-site, simply omit the style attribute from the DIV-tags. So what was < DIV style='display:none' > simply becomes < DIV >. And of course if you're into details, you should change the images in front of the items from plus.gif to minus.gif. If you're using the scripts that generate a tree from a database, you should hack it the other way around, because that sample is initially expanded. It's quite straightforward, but when I find the time I could post 2 versions of the samples, one collapsed and the other one expanded.

How can I liven up the treeview to make it look better? Well, I've been working on a pure-HTML extension of the treeview with a colleague of mine, which draws lines in the treeview, like MFC treeviews do. I will post this version shortly.

Cross-browser treeview?

I developed this treeview for Internet Explorer 5, which is not such a good idea if you want to develop a cross-browser treeview because Microsoft has the tendency to build all kind of fancy non-standard goodies into it's browser. Luckily some people on the CodeProject have tested it on other browsers and it only seemed to work on IE5 (not even IE4!). I'm now in the process of evaluating what I can do to improve this. Today (June 13th 2001) I checked all my script-entries against the actual standards and found out that all of it should work on browsers supporting CSS Level 1, HTML 4.0 and DOM Level 2. I made some modifications today as well and I'm curious on which browsers the script works correctly.

The treeview has been tested on the following browsers:

Microsoft Internet Explorer 4 (SP 2)Doesn't Work!
Microsoft Internet Explorer 5.0 (for Macintosh)Work partially (different look)
Microsoft Internet Explorer 5.00.3103.1000Works!
Microsoft Internet Explorer 5.50.4522.1800C0Works!
Microsoft Internet Explorer 6.00.2462.0000Works!
Netscape 4.77Doesn't work!
Netscape 6Works!
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; 0.8.1) Gecko/20010323Works!
Opera 5.0 (Build 528 on Windows 2000)Doesn't work!
Opera 6Doesn't work!

(Thanks to Jorge Sabater Redondo, Alexandra Berg, Neal Costello, Zinggl Alois, Jens Kreiensiek, Arnt Witteveen, Jamie Nordmeyer, Bryan Pietrzak and Bruce for the testing!)

If you have another browser than listed above, please test the script at THIS live-demo site and mail me to tell me if it did or didn't work on your browser... Together we can build a better treeview, I know it! Wink | ;-)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

D.D. de Kerf
Web Developer
Netherlands Netherlands
I completed college Higher Informatics (HIO Breda, The Netherlands) in august 2000. I have been software engineer/documentalist/project manager with Bodégro Technical Automation since 1998 (started as part-time, full-time since august 2000).

Lots of experience with Delphi, Visual C++, PL/SQL, ASP and PHP. Started a succesfull knowledge-sharing initiative in late 1999 called 'Cohort Forum', which resulted in a web-site a lot like the codeproject.

You may also be interested in...

Comments and Discussions

QuestionHREF Pin
Michael Janulaitis18-Jan-12 12:49
memberMichael Janulaitis18-Jan-12 12:49 
QuestionWorks In Chrome Pin
Daaron11-Jul-11 8:02
memberDaaron11-Jul-11 8:02 
GeneralIE8 Pin
Ze2103826-May-10 5:03
memberZe2103826-May-10 5:03 
GeneralThank you Pin
OmarGamil31-Oct-09 23:57
memberOmarGamil31-Oct-09 23:57 
pogramadox9-Oct-09 10:05
memberpogramadox9-Oct-09 10:05 
Questionadd href? Pin
neil.guo4-Nov-07 2:25
memberneil.guo4-Nov-07 2:25 
GeneralPerformance Pin
jiggyman21-May-07 16:29
memberjiggyman21-May-07 16:29 
GeneralRe: Performance Pin
Entropy23-May-07 4:19
memberEntropy23-May-07 4:19 
GeneralOne node expanded at time Pin
Lina_7919-Apr-07 5:38
memberLina_7919-Apr-07 5:38 
NewsNew Version Pin
AndrewVos9-Nov-06 23:18
memberAndrewVos9-Nov-06 23:18 
GeneralRe: New Version Pin
LTSpeed12-Nov-06 14:56
memberLTSpeed12-Nov-06 14:56 
General!!!!How to make code work with FireFox!!!! Pin
AndrewVos6-Nov-06 2:50
memberAndrewVos6-Nov-06 2:50 
GeneralUsing event object with tree Pin
atomicfroman14-Jun-06 11:08
memberatomicfroman14-Jun-06 11:08 
GeneralThe way of simplicity is often the best way... Pin
achainard16-Apr-06 21:47
memberachainard16-Apr-06 21:47 
GeneralGreat work Pin
prettymeshedup11-Apr-06 7:02
memberprettymeshedup11-Apr-06 7:02 
QuestionTree Performance? Pin
karba3010-Jan-06 19:51
memberkarba3010-Jan-06 19:51 
AnswerRe: Tree Performance? Pin
rajdeeep14-Feb-11 23:50
memberrajdeeep14-Feb-11 23:50 
GeneralRetain the tree navigation path Pin
Anonymous9-Sep-05 10:39
sussAnonymous9-Sep-05 10:39 
GeneralRe: Retain the tree navigation path Pin
CodeAddiction22-Dec-05 10:17
memberCodeAddiction22-Dec-05 10:17 
GeneralTHX so much ! Pin
KahL18-May-05 7:56
memberKahL18-May-05 7:56 
GeneralMy first thought - this code is is great and is so horribly bloated Pin
ZachJ16-May-05 17:02
memberZachJ16-May-05 17:02 
GeneralThe HTML Pin
Ramon Tristani29-Apr-05 22:15
memberRamon Tristani29-Apr-05 22:15 
GeneralA helpful article! Pin
HTML&JavacriptLearner23-Mar-05 18:06
sussHTML&JavacriptLearner23-Mar-05 18:06 
GeneralRe: A helpful article! Pin
HTML&JavascriptLearner23-Mar-05 19:09
sussHTML&JavascriptLearner23-Mar-05 19:09 
GeneralHTML treeview Pin 19:36 19:36 
GeneralMozilla 1.6, Netscape 7.1 support Pin
evaki22-Jan-05 7:57
memberevaki22-Jan-05 7:57 
GeneralExpand/Collapse All Pin
dsacker19-Nov-04 7:26
memberdsacker19-Nov-04 7:26 
GeneralRe: Expand/Collapse All Pin
dsacker19-Nov-04 7:48
memberdsacker19-Nov-04 7:48 
GeneralRe: Expand/Collapse All Pin
gewe21-Jun-05 3:01
membergewe21-Jun-05 3:01 
dsacker wrote:
At the bottom of my html, I then perform the function Toggle_All(), to set them to display=none. Otherwise, the expand/collapse will not work right.

How do you do that? In the onclick=Toggle_All(this), "this" is the node to be passed. How do you identify at the bottom of the HTML the node to be passed as the parameter for Toggle_All?
GeneralRe: Expand/Collapse All Pin
dsacker21-Jun-05 4:24
memberdsacker21-Jun-05 4:24 
GeneralRe: Expand/Collapse All Pin
Anonymous5-Jan-05 4:34
sussAnonymous5-Jan-05 4:34 
GeneralRe: Expand/Collapse All Pin
henrikmunthebrun7-Jun-05 22:10
memberhenrikmunthebrun7-Jun-05 22:10 
GeneralRe: Expand/Collapse All Pin
gewe21-Jun-05 2:53
membergewe21-Jun-05 2:53 
GeneralRe: Expand/Collapse All Pin
karba3010-Jan-06 21:21
memberkarba3010-Jan-06 21:21 
GeneralRe: Expand/Collapse All Pin
Mike Tours24-May-07 7:13
memberMike Tours24-May-07 7:13 
GeneralAdding a click on description Pin
sanrek23-Jun-04 3:54
membersanrek23-Jun-04 3:54 
GeneralRe: Adding a click on description Pin
rajeevking22-Feb-05 0:45
memberrajeevking22-Feb-05 0:45 
GeneralRe: Adding a click on description Pin
gcsnb28-Mar-06 6:48
membergcsnb28-Mar-06 6:48 
GeneralCross Browser Pin
click.ok6-May-04 6:14
memberclick.ok6-May-04 6:14 
GeneralDeep links in treeview Pin
thilol3-May-04 6:54
memberthilol3-May-04 6:54 
GeneralBrowser Compatibility Pin
Stephen Robin22-Apr-04 10:21
sussStephen Robin22-Apr-04 10:21 
GeneralSimple treeview not working Pin
rjv_rnjn23-Mar-04 5:39
sussrjv_rnjn23-Mar-04 5:39 
GeneralGetting parentElement using dom Pin
devikiran15-Mar-04 20:59
memberdevikiran15-Mar-04 20:59 
GeneralRe: Getting parentElement using dom Pin
D.D. de Kerf15-Mar-04 21:40
memberD.D. de Kerf15-Mar-04 21:40 
GeneralRe: Getting parentElement using dom Pin
Anonymous23-Jan-05 15:33
sussAnonymous23-Jan-05 15:33 
GeneralBrilliant - just a couple of changes Pin
George Nicoll14-Dec-03 8:07
memberGeorge Nicoll14-Dec-03 8:07 
GeneralCheeky! Pin
LeeDaviesVBSource8-Oct-03 3:52
memberLeeDaviesVBSource8-Oct-03 3:52 
GeneralRe: Cheeky! Pin
Anonymous21-Jun-05 8:33
sussAnonymous21-Jun-05 8:33 
GeneralRe: Cheeky! Pin
Spiff Dog21-Jun-05 8:35
memberSpiff Dog21-Jun-05 8:35 
Generalgenius! Pin
acatalept20-Sep-03 1:35
memberacatalept20-Sep-03 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151120.1 | Last Updated 22 Feb 2002
Article Copyright 2001 by D.D. de Kerf
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid