Adding a tab looks like it should be something done as a function rather than inline, so you could write it like this
// i is the last id
var addTab = (function (tab_root, nav_root, i) { // IIFE to create closure
return function (title, contents) {
var li, a, div, j;
// get id
j = ++i; // I put it in it's own var to avoid race conditions
// create nav
li = document.createElement('li');
a = document.createElement('a');
a.setAttribute('href', '#tabs-' + j);
if (title) a.appendChild(document.createTextNode(title));
li.appendChild(a);
// create tab
div = document.createElement('div');
if (contents) div.appendChild(document.createTextNode(contents));
// append to document
tab_root.appendChild(div);
nav_root.appendChild(li);
return {tab: div, nav: a, id: j};
}
}(document.getElementById('tabs'), document.getElementById('ul_id'), 3)());
// invoke IIFE with initials i.e. id 3 for 3 tabs existing
now
addTab('tab 4', 'tab 4 contents');
An Object is returned with properties tab, nav and id. This lets you do more work with the different parts as required.