Click here to Skip to main content
12,826,440 members (24,869 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 5 Oct 2013

Creating Treeview in jQuery

, 5 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to create Treeview in jQuery


In this article, we will learn how to create a treeview in jQuery. Treeview is used for representing hierarchy. You can use it for many purposes. So let’s create it.

Basics – Understanding Tree View

For creating treeview, we will use the following two methods:


It is used for binding click event handler on an element. Whenever user clicks on any element, it will be detected by using click handler. Syntax for click is:;


It is a jQuery UI method that is used for toggling the display property of an element with the animation effect as well. For our purpose, we will use toggle with fold effect. The syntax for toggle is:


In HTML, we will use the following tags:

UL – Unordered List

It is used for declaring unordered list.

<ul> List items </ul>

LI – List Item

It is used for declaring list items.

<li> Item name </li>

Logic behind the JavaScript Tree View

First, we need to prepare the HTML.

In treeview, we have Parent Nodes, Child Nodes, Nodes.

To create nodes, we will use the LI and UL tags of HTML.

To represent the parent node, we will use one LI tag just above the UL.

To represent the child node, we use the UL tag just below the LI tag.

Coding the Treeview

It’s time to turn our dreamed Treeview into reality.

To create treeview items, add the following HTML snippet in your HTML file.

<li id="m1">Main 1</li>
<div id="l1cover">
<li id="inLst">2</li>
<div id="inCt1">
<li id="m2">Main 2</li>
<div id="l2cover">
<li id="inLst2">1</li>
<div id="inCt2">

Now to make your treeview work, just add the following jQuery snippet:

 $("#m1").click(function () {
$("#l1cover").toggle("fold", 1000);
$("#m2").click(function () {
$("#l2cover").toggle("fold", 1000);
$("#inLst").click(function () {
$("#inCt1").toggle("fold", 1000);
$("#inLst2").click(function () {
$("#inCt2").toggle("fold", 1000);


To summarize, we just learned how to create the treeview in jQuery. Here the important point is not about creating the treeview but how to use the power of jQuery smartly. You can check my other articles on jQuery for more details on how to use it.

Thanks for reading and don’t forget to comment and like this article.


Live demo

Full Tree View


Minimized 1st sub node


Minimized 2nd child node


Minimized child nodes


Minimized 2nd node


Minimized 1st node


All nodes minimized


Filed under: CodeProject, jQuery


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Arpit Jain
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionTreeview not working Pin
Member 113827848-Jun-16 1:05
memberMember 113827848-Jun-16 1:05 
GeneralMy vote of 1 Pin
Er. Puneet Goel8-May-14 19:54
professionalEr. Puneet Goel8-May-14 19:54 
GeneralRe: My vote of 1 Pin
Arpit Jain9-May-14 9:49
memberArpit Jain9-May-14 9:49 
GeneralRe: My vote of 1 Pin
Er. Puneet Goel12-May-14 19:28
professionalEr. Puneet Goel12-May-14 19:28 
GeneralRe: My vote of 1 Pin
Lin Zang22-May-14 17:05
memberLin Zang22-May-14 17:05 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 5 Oct 2013
Article Copyright 2013 by Arpit Jain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid