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

  • Home
  • SEARCH
  • 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 7160309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:22:42+00:00 2026-05-28T13:22:42+00:00

Context I have a tree view with folders and files: Additional info All files

  • 0

Context

I have a tree view with folders and files:

enter image description here

Additional info

  • All files and folders are draggable and droppable.
  • Folders have .pft-directory generic class and #/folder1/folder2/etc id.
  • Files have .pft-file generic class and files/folder1/folder2/etc/fileName.ext href.
  • Files and empty folders are <li><a>text of file/folder</a><li>.
  • !empty folders have in addition <ul><li><a>text of subfolder</a></li>...etc...</ul>.

Issue

After each drag and drop, I update folder’s id and file’s href. Obviously it becomes more complicated when a folder isn’t empty and there has several iterations of subfolders. I wrote 2 iterations (see the following code).

Question

Can you help me to write a DOM recursive reading function?

Code

jquery:

//the first folder
ui.draggable.find('a.pft-directory:first').attr('id', $path_Ite1);

//inside the first folder...

ui.draggable.find('ul:first > li > a').each(function() {
    var $this_Ite1 = $(this);
    
    //if file, else folder      

    if ($this_Ite1.hasClass('pft-file')) {
        $this_Ite1.attr('href', 'files' + $path_Ite1 + '/' + $this_Ite1.text());
    }
    else {
        var $path_Ite2 = $path_Ite1 + '/' + $this_Ite1.text(); 
        $this_Ite1.attr('id', $path_Ite2);
        
        //inside the second folder...

        $this_Ite1.parent().find('ul:first > li > a').each(function() {
            var $this_Ite2 = $(this);

            if ($this_Ite2.hasClass('pft-file')) {
                $this_Ite2.attr('href', 'files' + $path_Ite2 + '/' + $this_Ite2.text());
            }
            else {
                var $path_Ite3 = $path_Ite2 + '/' + $this_Ite2.text(); 
                $this_Ite2.attr('id', $path_Ite3);

                //inside the third folder etc...

            }
        });
    }
});
  • 1 1 Answer
  • 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-05-28T13:22:42+00:00Added an answer on May 28, 2026 at 1:22 pm

    Personally, I can’t help you with the draggable part, but with regards to the recursive function you might find something like the following useful.

    I also had to assume what your HTML looks like based on the description you gave (some HTML would be real handy in these types of questions).

    You can see it in action at the following jsFiddle. (If using Firefox, select everything on the Results pane, right-click and view selection source to see the updated attributes)

    HTML:

    <ul>
        <li>
            <a class="ptf-directory">Folder1</a>
            <ul>
                <li>
                    <a class="ptf-directory">Folder3</a>
                    <ul>
                        <li>
                            <a class="ptf-file">File4.txt</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a class="ptf-file">File1.txt</a>
                </li>
            </ul>
        </li>
        <li>
            <a class="ptf-directory">Folder2</a>
        </li>
        <li>
            <a class="ptf-file">File2.txt</a>
        </li>    
        <li>
            <a class="ptf-file">File3.txt</a>
        </li>
    </ul>
    

    JQuery:

    $(function(){
        UpdateFolderItems($('ul:first'), '');
    });
    
    function UpdateFolderItems(folder, basePath)
    {
        // go through folder contents
        folder.children('li').each(function(){
    
            // get item
            var item = $(this).children('a');
    
            // generate new path for item
            var newPath = basePath + '/' + item.text();
    
            // if item is a folder
            if (item.hasClass('ptf-directory'))
            {   
                // update id on folder
                item.attr('id', newPath);
    
                // if folder content exists
                var folderContent = $(this).children('ul:first');
                if (folderContent.length > 0)
                {
                    // update folder content
                    UpdateFolderItems(folderContent, newPath);   
                }
            }
    
            // if item is a file
            else if (item.hasClass('ptf-file'))
            {   
                // update href on file
                item.attr('href', newPath);
            }
    
        });                         
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context I have a tree view with folders ( .directory ): When I mouseover
I have a context loader class which loads an XML file with info on
I have three tree view controls which house different (but mostly similar data), as
I have a tree-view and datagridview on my form. I will have an import
I have a basic file system tree that consists of two tables: folders =======================
i would like to have a multiple directory view while using QDirModel and qtreeview
I have content management system application that uses a polymorphic tree table as the
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
Context We have a SharePoint site set up on a Windows Server 2008 VM
Setting up ASP.net MVC with Linq2SQL or Entity Framework's context to have scaffolding work

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.