1

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.

1
  • 1
    What js framework are you using? Post more details in the question. Commented Feb 20, 2010 at 12:34

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

jQuery is actually a Javascript library, which makes Javascript a little easier to write: jquery.com
can u pls provide in javascript because im totally new in javascript as well treeview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.