2

I am trying to call the delete button to remove the listed file. Can anyone help me to build the logic.

$(document).ready(function () {
    $('input[type = "file"]').change(function (e) {
        var input = document.getElementById('fileUploader');
        var output = document.getElementById('divFiles');
        var HTML = "<table>";
        for (var i = 0; i < input.files.length; ++i) {
            HTML += "<tr><td>" + input.files.item(i).name + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button ></button></td></tr>";
        }
        HTML += "</table>";
        output.innerHTML = HTML;
    });
});

enter image description here

4
  • 1
    Try this : $('table').on('click', 'input[type="button"]', function(e){ $(this).closest('tr').remove() }) Commented Apr 21, 2018 at 7:04
  • it's not working Commented Apr 21, 2018 at 7:13
  • got the result thank you for your idea $(document).ready(function () { $(document).on('click', "button", function (e) { $(this).closest('tr').remove(); }); }); Commented Apr 21, 2018 at 7:20
  • I am facing an another issue after selecting the files from browser again while re selecting the file the existing files were not listing. Commented Apr 21, 2018 at 7:41

1 Answer 1

1

You can remove the parent tr tag containing button delete like

$('.btnDelete').on('click', function () {
  $(this).closest('tr').remove();
});
Sign up to request clarification or add additional context in comments.

8 Comments

$(document).ready(function () { $(document).on('click', "button", function (e) { $(this).closest('tr').remove(); }); });
I am facing an another issue after selecting the files from browser again while re selecting the file the existing files were not listing.
@KavithaPrabhu input.files will be reset if you reupload files. If you want to keep the existing files, you can store those files in an array then rendering files from that array instead
<script> updateList = function () { var input = document.getElementById('fileUploader'); var output = document.getElementById('divFiles'); var filelist = new Array(); var HTML = "<table>"; for (var i = 0; i < input.files.length; ++i) { filelist[i] = input.files.item(i).name; HTML += "<tr><td>" + filelist[i] + "</td><td><button ></button></td></tr>"; } HTML += "</table>"; output.innerHTML = HTML; }
i am trying like the above code still it's not working
|

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.