Roberto Flores
Roberto Flores

Reputation: 789

Collapsing Tree not working

I am trying to create a collapsing tree and was able to find the answer here.

However, when I click on the folder, it doe not collapse and I am not sure why the second item does not appear.

$('.folder').on('click', function(e) {
		  var folder = $(this).find('.sub-folder');
			  if (e.target !== this) return;
			
			  if(folder.hasClass('hidden1')) {
				folder.removeClass('hidden1');
			  } else {
				folder.addClass('hidden1');
			  }
	});
.folder {
  		cursor: pointer;
	}
	.sub-folder{
		cursor:pointer;	
	}
<ul class="container" style="list-style:none; margin-left:-14px">
	                   <li class="folder">
                       		<h4><a href="#" ><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h4>
                            <ul class="sub-folder hidden1" style="list-style:none">
                            	<li>
                                	<h5><a href="DMgmt.cfm"><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h5>
                                </li>
                            </ul>
                       </li> 
                   </ul>

UPDATE

The following i the toggle and it does not work out for me:

var toggle = function () {

        $(this).parent().children().toggle();
        $(this).toggle();

    };

$(".Collapsable").click(toggle);
$(".Collapsable").each(toggle);
	});
<ul style="list-style:none; margin-left:-14px">
	                   <li class="Collapsable">
                       		<h4><a href="#" ><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h4>
                            <ul style="list-style:none">
                            	<li class="Collaposable" >
                                	<h5><a href="DMgmt.cfm"><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h5>
                                </li>
                            </ul>
                       </li> 
                   </ul>

and shows the following image:

enter image description here

However, it appears the hidden folder already. Not when the user clicks on the button and then the second files appears

UPDATE 2

Below is what I and it still does not show the hidden link

// JavaScript Document$('.folder').on('click', function(e) {
$(document).on('click', '.folder', function(){
  var folder = $(this).find('.sub-folder');
  folder.toggleClass('hidden1');
});
.folder {
  cursor: pointer;
}
.sub-folder {
  cursor: pointer;
}
.hidden1 {
  display: none;
}
<link rel="stylesheet" href="images/styleregal.css" type="text/css" />
<script src="js/Collaposable_tree.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<ul class="container" style="list-style:none; margin-left:-14px">
	                   <li class="folder">
                       		<h4><a href="#" ><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h4>
                            <ul class="sub-folder hidden1" style="list-style:none">
                            	<li class="Collapsable" >
                                	<h5><a href="CalMediConnect_DMgmt.cfm"><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h5>
                                </li>
                            </ul>
                       </li> 
                   </ul>

Upvotes: 0

Views: 45

Answers (1)

Adjit
Adjit

Reputation: 10305

You are over complicating this. You don't need any of those if statements. The point of jQuery is to simplify all of this and instead just use something like .toggleClass() instead of an if-else.

Also, it seems like you do not have a CSS attribute telling any element with the class hidden1 to be hidden.

See the edits below and it should work as desired.

$('.folder').on('click', function(e) {
  var folder = $(this).find('.sub-folder');
  folder.toggleClass('hidden1');
});
.folder {
  cursor: pointer;
}
.sub-folder {
  cursor: pointer;
}
.hidden1 {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="container" style="list-style:none; margin-left:-14px">
  <li class="folder">
    <h4><a href="#" ><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h4>
    <ul class="sub-folder hidden1" style="list-style:none">
      <li>
        <h5><a href="DMgmt.cfm"><img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left"/ >&nbsp;Disease Management</a></h5>
      </li>
    </ul>
  </li>
</ul>

Upvotes: 2

Related Questions