tags to hold the root nodesthe nodes that are always visible. I'll use tags to hold the branches and leaves of the treethe nodes that need to be toggled. Write the following HTML between the tags of the HTML shell:
The HTML is not complicated. Each root node contains an image representing a closed folder and some text. Following each root node is a branch whose class attribute is set to "branch." This "hides" the branch. A JavaScript event handler (see following page) adds the functionality to "show" the branch and display an open folder icon. Also note that branch No. 4, includes a child node that is also a trigger. Initially, the child trigger will be hidden but will be clickable when displayed. Finally, I've represented end nodes (leaves) using the doc.gif graphic and some display text which lets users know that they've reached a leaf node rather than another branch. Figure 1 shows how the populated tree looks in IE.
Figure 1: The populated JavaScript tree control in Internet Explorer.
When creating your own trees for Web pages, you'd typically wrap the image and text inside a hyperlink, to take some action when users click an item.
Problem: Screen real estate is at a premium on many Web pages. Developers need a way to present a lot of information in a limited space. But users need familiar user interface conventions. How do you get more information in a smaller space without confusing end users?
Solution: Build a familiar tree control using some simple style rules, a few graphics, and some JavaScript.
HTML and JavaScript-based trees are becoming more common every day. Are you using tree controls already? Are you using a commercial version or did you roll your own. What do you think of this article's approach to implementing a tree control? Join the discussions at Web.dhtml.general or Web.dhtml.scripting to get answers, make comments, or help others with their problems.