Add elements and remove a specific one that has been added via Javascript
<p>I have the following code that adds a file upload option to a div and will remove the last element added. </p>

<pre><code>&lt;script language="javascript" type="text/javascript"&gt;
    function add(type) {
        var element = document.createElement("input");
        element.setAttribute("type", type);
        element.setAttribute("value", type);
        element.setAttribute("name", type);
        element.setAttribute("style", "width: 500px;");
        var newfile = document.getElementById("uploadhere");
        newfile.appendChild(element);           
    }
    function remove() {
        var newfile = document.getElementById("uploadhere");
        newfile.removeChild(newfile.lastChild);
    }

&lt;/script&gt;
</code></pre>

<p>I am trying to figure out a way to remove a specific element added instead of just removing the last element probably with an index somehow. Or even just having a remove button next to the element created. </p>

<p>edited to add my solution</p>

<p>This is how I solved the issue </p>

<pre><code>&lt;script language="javascript" type="text/javascript"&gt;
        var i = 0;
        function add(type) {            
            var element = document.createElement("input");
            element.setAttribute("type", type);
            element.setAttribute("value", type);
            element.setAttribute("name", type);
            element.setAttribute("style", "width: 500px;");
            element.setAttribute("id", "element-" + i);
            var removebutton = document.createElement("input");
            removebutton.type = "button";
            removebutton.value = "Remove";
            removebutton.setAttribute("id", "remove-" + i);
            removebutton.setAttribute("onclick", "remove(" + i + ")");
            var newfile = document.getElementById("uploadhere");
            newfile.appendChild(element);
            newfile.appendChild(removebutton);
            i++;                     
        }
        function remove(id) {            
            document.getElementById("uploadhere").removeChild(document.getElementById("element-" + id));            
            document.getElementById("uploadhere").removeChild(document.getElementById("remove-" + id));
        }

    &lt;/script&gt;
</code></pre>
