user274139
user274139

Reputation: 91

control an ASP.Net treeview in Javascript

How can I write Javascript to expand and collapse a particular parent of a TreeView when i click on the parent node?

If one of the parent nodes is expanded, then if I click on that it should collapse; and if already collapsed, then vice versa.

Upvotes: 1

Views: 674

Answers (1)

Harmen
Harmen

Reputation: 22438

It's as easy as:

<ul id="tree">
    <li>
        <span class="title">Title</span>
        <ul>
            <li><span class="title">Item 1</span></li>
            <li><span class="title">Item 2</span></li>
            <li><span class="title">Another item</span>
                <ul>
                    <li><span class="title">another one</span></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

$(document).ready(function(){
    $('#tree span.title').click(function(){
        $(this).next().toggle();
    });
});

When using jQuery.

Here is an example

Upvotes: 3

Related Questions