tab is not displaying propely
Im creating a dynamic tab, shown below is the code.
- function addTab(tabCounter, tabName)
- {
- var i = tabCounter;
- var tabTitle = tabName;
- var a_el=document.createElement('a');
- a_el.setAttribute("href", "#tabs-" + i);
- a_el.appendChild(document.createTextNode(tabTitle));
- var li_el = document.createElement('li');
- li_el.appendChild(a_el);
-
- document.getElementById("ul_id").appendChild(li_el);
-
- var par = document.createElement('p');
- par.appendChild(document.createTextNode("content for added node " + i));
-
- //add tab
- var dib = document.createElement('div');
- dib.setAttribute("id","tabs-"+i);
- dib.appendChild(par);
-
- var test=document.getElementById("tabs")
- test.appendChild(dib);
- }
- function provideData()
- {
- for(var i=1; i<5; i++){
- addTab("data" +i , "anotherdata"+i);
- }
- }
However I get weird result, the tab title is not divided and the tab contents are displayed all at once.
The output is shown below.
But when I put the entire code for adding tab inside a for loop to test if it functions properly, the code is working flawlessy.
- function addTab()
- {
- for(x=1; x<4; x++)
- {
- [code to create tab]
- .....
- .....
- }
- }
WHat do I need to do to make the code in the upper part of the page work perfectly? And what's the difference between these the two that results to a different output?