I need a help on collapse and Expand using Javascript. Here is my running code (.html)
<h2>Test</h2> <html lang="en"> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-16"> <title></title> <script type="text/javascript"> function toggleDisplay(element) { element.style.display = element.style.display === 'none' ? '' : 'none'; }; function toggleDisplayAll(elements) { for(var i=0; i<elements.length; i++) { toggleDisplay(elements[i]); } } </script> </head> <body> <ul> <a onclick="toggleDisplayAll(this.parentNode.getElementsByTagName('ul')); return false;" href="#">Name:</a> <ul style="display:none;"> <a onclick="toggleDisplayAll(this.parentNode.getElementsByTagName('ul')); return false;" href="#">Address: </a> <ul style="display:none;"> <a onclick="toggleDisplayAll(this.parentNode.getElementsByTagName('li')); return false;" href="#">Subject: </a> <ul style="display:none;"> <li style="display:none;">Id </li> </ul> </ul> </ul> </ul> </body> </html>
If you run this html, you will get out put as
Name
on click of Name, it is showing all the child elements
Name: Address: Subject:On click of Subject it is showing Id
Name: Address: Subject: . IdWhat i want here is each child should open only on parent click.
When run the html, only Name will dispaly
Name:On click of Name, only Address will be displayed as a child element.
Name: Address:
Onclick of Address, only Subject will display
Name: Address: Subject:
Than finally on click of Subject, id will show up
Name: Address: Subject: . Id
How to implement this tree structure. what i am doing wrong here. please suggest me.