Q:

How to dynamically create list of  tags using js

I am creating html page which needs to create a list of links dynamically on a click of button. I know how to create this list when number of links to be created is known before like this:
    //For 4 tags:
    var mydiv = document.getElementById("myDiv");
    var aTag = document.createElement('a');
    aTag.innerHTML = "link1 text";
    aTag.setAttribute('onclick',"func()");
    mydiv.appendChild(aTag);
    var bTag = document.createElement('b');
    bTag.innerHTML = "link2 text";
    bTag.setAttribute('onclick',"func()");          
    mydiv.appendChild(bTag);
    var cTag = document.createElement('c');
    cTag.innerHTML = "link3 text";
    cTag.setAttribute('onclick',"func()");
    mydiv.appendChild(cTag);
    var dTag = document.createElement('d');
    dTag.setAttribute('onclick',"func()");
    dTag.innerHTML = "link4 text";
    mydiv.appendChild(dTag);

But the problem is that the count will be known at run time and also on function call i need to identify the id of link that invoked function.. Can anybody help? 

A:

I don't know weather you receive or not the HTML to be shown in the anchor, but anyway, this should do the work:
function createAnchor(id, somethingElse) {
    var anchor = document.createElement('a');
    anchor.innerHTML = "link" + id + " text";
    anchor.setAttribute("onclick", "func()");
    return anchor;
}

Then you call the function like this:
function main(num_anchors) {
    var mydiv = document.getElementById("myDiv");
    for (var i = 0; i < num_anchors; i += 1) {
        mydiv.appendChild(createAnchor(i));
    }
}

Of course this code can be improved, but this is just for show how can this be possible.

