Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

The below code gives a Treeview of Items and their sub-items,.. It shows in the hierarchical format,... but instead of hard-coding the input Item 1 , Sub-item 1 etc,..., i wanna give an input so that it will become a generic code and show the hierarchical tree view for any input we give,..
I tried giving some variables but it didn't work for me..
1)Could you please suggest me on this.. and also do u have any other ideas to build a hierarchical tree using HTML/JAVASCRIPT..
2)I also tried using DOM but it didn't work,.. I didn't know how to use DOM properly, suggest me if you have any idea about DOM

<code>
<ul>
<li>
    <a href="#" onclick="toggle('node1')">Item 1</a>
    <ul id="node1" style="display:none">
      <li>Sub-item 1</li>
      <li>
        <a href="#" onclick="toggle('node2')">Sub-item 2</a>
        <ul id="node2" style="display:none">
          <li>Sub-sub-item 1</li>
          <li>Sub-sub-item 2</li>
        </ul>
      </li>
      <li>Sub-item 3</li>
    </ul>
  </li>
  <li>
    <a href="#" onclick="toggle('node3')">Item 2</a>
    <ul id="node3" style="display:none">
      <li>Sub-item 1</li>
      <li>Sub-item 2</li>
    </ul>
  </li>
  <li>Item 3</li>
</ul>

</code>

Thanks in advance!
Posted
Updated 23-Jun-10 16:41pm
v2

You need to create elements dynamically and add them at particular control.

Like find the <ul> using its id and create a <li> and add it to the <ul>

You can check following article to know how to add controls dynamically using javascript.

http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/
 
Share this answer
 
Comments
Swathi@MST 24-Jun-10 1:39am    
Thanks a lot for your reply! I have checked the above link to add controls dynamically,.
There was some problem in the code,. Its showing "Add some elements" as the o/p,. but not showing the childs added to it,..
The fuction addElement has not been triggered..






function addElement()
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number '+num+' has been added! Remove the div "'+divIdName+'"' ni.appendChild(newdiv);
}




Add Some Elements





Please identify the error in the code.
Thanks in advance!
Thanks a lot for your reply! I have checked the above link to add controls dynamically,.
There was some problem in the code,. Its showing "Add some elements" as the o/p,. but not showing the childs added to it,..
The fuction addElement has not been triggered..

<pre>
<html>
<body>
<script type="text/javascript">
function addElement()
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick = \'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>' ni.appendChild(newdiv);
}
</script>

<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>

</body>
</html>
</pre>

Please identify the error in the code.
Thanks in advance!
 
Share this answer
 
v2
Comments
PSK_ 24-Jun-10 1:59am    
";" was missing in the function.

Try this


<html>
<body>
<script type="text/javascript">
function addElement()
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number ' + num +' has been added! <a href='#' onclick = 'removeElement('+divIdName

+')'>Remove the div "'+divIdName+'"</a>' ;
ni.appendChild(newdiv);
}
</script>

<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>

</body>
</html>
Swathi@MST 24-Jun-10 2:10am    
Thanks for your reply,. I tried the above code which you have provided ,. but still i could see the same problem, no childs added,.. It shows only "Add Some Elements" as the o/p,. when i click it, its not showing any childs below..
Please suggest me on this
Thanks for your reply,. I tried the above code which you have provided,. but still i could see the same problem, no childs added,.. It shows only "Add Some Elements" as the o/p,. when i click it, its not showing any childs below..
Please suggest me on this
 
Share this answer
 
v2
Comments
PSK_ 24-Jun-10 2:45am    
Find this in the code you have posted

newdiv.innerHTML = 'Element Number '+num+' has been added! <a href='#' onclick = 'removeElement('+divIdName+')'>Remove the div "'+divIdName+'"</a>'

there is a semicolon missing add it.
Hi Prakash,

I have added the semicolon after that line, but still facing the same problem. Did u try executing the program.. What was the O/p..?
You have suggested to try this code, i tried the same,.. i inspected every line, seems to be correct,.

<code>
<html>
<body>
<script type="text/javascript">
function addElement()
{
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = 'Element Number ' + num +' has been added! <a href='#' onclick = 'removeElement('+divIdName+')'>Remove the div "'+divIdName+'"</a>' ;
ni.appendChild(newdiv);
}
</script>

<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>

</body>
</html>
</code>

I have pasted the same code,.
Thanks in advance.

Awaiting your reply,
Swathi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900