Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459469

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:20:19+00:00 2026-06-10T13:20:19+00:00

I ran into the excellent jstree jQuery UI plug in this morning. In a

  • 0

I ran into the excellent jstree jQuery UI plug in this morning. In a word – great! It is easy to use, easy to style & does what it says on the box. The one thing I have not yet been able to figure out is this – in my app I want to ensure that only one node is expanded at any given time. i.e. when the user clicks on the + button and expands a node, any previously expanded node should silently be collapsed. I need to do this in part to prevent the container div for a rather lengthy tree view from creating an ugly scrollbar on overflow and also to avoid “choice overload” for the user.

I imagine that there is some way of doing this but the good but rather terse jstree documentation has not helped me to identify the right way to do this. I would much appreciate any help.

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:20:21+00:00Added an answer on June 10, 2026 at 1:20 pm

    jsTree is great but its documentation is rather dense. I eventually figured it out so here is the solution for anyone running into this thread.

    Firstly, you need to bind the open_node event to the tree in question. Something along the lines of

    $("tree").jstree({"themes":objTheme,"plugins":arrPlugins,"core":objCore}).
    bind("open_node.jstree",function(event,data){closeOld(data)}); 
    

    i.e. you configure the treeview instance and then bind the open_node event. Here I am calling the closeOld function to do the job I require – close any other node that might be open. The function goes like so

    function closeOld(data)
    {
        var nn = data.rslt.obj;
        var thisLvl = nn;
        var levels = new Array();
        var iex = 0;
        while (-1 != thisLvl)
        {
            levels.push(thisLvl);
            thisLvl = data.inst._get_parent(thisLvl);
            iex++;
        }
    
        if (0 < ignoreExp)
        {
            ignoreExp--;
            return;
        }
    
        $("#divElements").jstree("close_all");
        ignoreExp = iex;
        var len = levels.length - 1;
        for (var i=len;i >=0;i--) $('#divElements').jstree('open_node',levels[i]);
    }
    

    This will correctly handle the folding of all other nodes irrespective of the nesting level of the node that has just been expanded.

    A brief explanation of the steps involved

    • First we step back up the treeview until we reach a top level node (-1 in jstree speak) making sure that we record every ancestor node encountered in the process in the array levels
    • Next we collapse all the nodes in the treeview
    • We are now going to re-expand all of the nodees in the levels array. Whilst doing so we do not want this code to execute again. To stop that from happening we set the global ignoreEx variable to the number of nodes in levels
    • Finally, we step through the nodes in levels and expand each one of them
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ran into a problem this morning. I wrote the following lines: var Markup =
Ran into this error message while trying to select some records off a table.
Ran into this problem today, posting in case someone else has the same issue.
Ran into this while converting a VB.NET interface to C#; the VB version defines
I ran into this error when I try to evaluate the last line below:
I ran into this supposed interview of Bjarne Stroustrup, the inventor of C++. http://artlung.com/smorgasborg/Invention_of_Cplusplus.shtml
Ran into problem with creating custom select dropdown plugin in jQuery. I'm at the
I ran into a bad chunk of code today that equates to this: [[asdf]].each
Ran into a bit of an issue with some jQuery code, but in essence,
Ran into this issue yesterday on one of our sites. First of all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.