четвъртък, 19 юли 2007 г.

Add and Remove HTML elements dynamically with Javascript

Here is some simple JavaScript. With some tweaking, you may fit it on your webpage.


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 = "Some contents";
ni.appendChild(newdiv);

// For IE, set any attributes directly;
// do not use the setAttribute method().
// E.g. use newdiv.style.zIndex = "1000" instead of
// newdiv.setAttribute('style','z-index: 1000;');
}

function removeElement(divNum)
{
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}

Няма коментари: