0

my html div part is

<div id="upload">

<input type="button" id="add" value="Click here to add" onclick="uploadFile();">

<input type="hidden" name="fileCount" id="fileCount" value="0" />
</div>

my javascript is

function uploadFile()
{
    var count = parseInt($('#fileCount').val(), 10);
    count = count + 1;
    if(count<=2){
        var x = document.createElement("INPUT");
        var br = document.createElement("br");
        var text = document.createElement("INPUT");
        var remove = document.createElement("INPUT");
        text.setAttribute("type", "text");
        text.setAttribute("name", "description_" + count);
        text.setAttribute("value", "file description");
        remove.setAttribute("type", "button");
        remove.setAttribute("value", "Delete");
        remove.setAttribute("id", "Delete"+count);
        remove.setAttribute("onclick", "remove();");

        x.setAttribute("type", "file");
        x.setAttribute("name", "file_" + count);
        x.setAttribute("id", "file_" + count);

        x.setAttribute("onchange","checkFile(this);");
        upload.appendChild(br);
        upload.appendChild(x);
        upload.appendChild(text);
        upload.appendChild(remove);
        $('#fileCount').val(count);
    }
    else{
        alert("cant upload more than two files");
    }
}

function remove()
{
    //  upload.getElementById(file_).remove();
}

Here I need a remove function for deleting the corresponding element when I click Delete button dynamically.

jasonscript
  • 6,039
  • 3
  • 28
  • 43
user3789344
  • 81
  • 1
  • 3
  • 14

2 Answers2

0

call this function on click of the button.

function removeDummy() { var elem = document.getElementById('elementtodelete'); elem.parentNode.removeChild(elem); return false; }

Vishal
  • 816
  • 11
  • 19
-1

You can use jQuery remove() method which delete the elements from HTML and DOM.

$(selector).remove();

jQuery remove method ref: remove()

Srinu Chinka
  • 1,471
  • 13
  • 19