Add and remove links dynamically
HTML
<button id="add">Add link</button>
<button id="remove">Remove link</button>
<ul id="list">
</ul>
JavaScript
var i = 0;
window.onload = init;
function init() {
document.getElementById('add').onclick = add;
document.getElementById('remove').onclick = remove;
}
function add() {
var ul = document.getElementById('list');
var li = document.createElement('li');
var a = document.createElement('a');
a.setAttribute('href', 'www.google.it');
a.innerText = (i + 1).toString();
li.appendChild(a);
ul.appendChild(li);
i++;
}
function remove() {
if (i == 0)
return;
var ul = document.getElementById('list');
ul.removeChild(ul.lastChild);
i--;
}