1

I have the following code that adds a file upload option to a div and will remove the last element added.

<script language="javascript" type="text/javascript">
    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);
    }

</script>

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.

edited to add my solution

This is how I solved the issue

<script language="javascript" type="text/javascript">
        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));
        }

    </script>
1
  • 1
    I like the idea of having a remove button next to the element created. Makes the removal logic easy to implement. Commented Jan 3, 2012 at 18:01

2 Answers 2

1

Try this:

<script language="javascript" type="text/javascript">
    var i = 1;
    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 newfile = document.getElementById("uploadhere");
        newfile.appendChild(element);           
    }
    function remove(id) {
        document.getElementById("uploadhere").removeChild(document.getElementById("element-" + i));
    }

</script>
Sign up to request clarification or add additional context in comments.

Comments

0

One option, like you said, is to use an index:

function remove(index)
{
  var newfile = document.getElementById("uploadhere");
  newFile.removeChild(newfile.childNodes[index])
}

And remove the newfile.removeChild line in your "add" function

Another option, like you said, is to use a button, which might be easier from a user friendliness standpoint. My preference is to put everything in a table to make it neater:

<table>
  <tbody id="uploadhere">
  </tbody>
</table>

<script>
  var newfile = document.getElementById("uploadhere");

  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;");

    // Create your button
    var button = document.createElement("button");
    button.onclick = "newfile.removeChild(this.parentNode.parentNode)";
    var text = document.createTextNode("Remove");
    button.appendChild(text);

    // Create a table row in which to put the data
    var row = newFile.insertRow(-1);

    // Create a table cell in which to put your "input" element
    var cell1 = row.insertCell(-1);
    cell1.appendChild(element);

    // Create a table cell in which to put your button
    var cell2 = row.insertCell(-1);
    cell2.appendChild(button);
  }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.