user1182476
user1182476

Reputation: 303

How to remove a selected file from the input file?

I have seen a flaw with the file input in html. When the user uses the file input to select a file then that is fine, but if the user wants to remove the selected file and want a blank file input, then the user cant do this. Does anyone know how to remove a selected file from the file input?

Below is my file input code in jquery:

var $imagefile = $('<input />')
    .attr({
        type: 'file',
        name: 'imageFile',
        id: 'imageFile'
    });

Upvotes: 1

Views: 6040

Answers (1)

Mohammad Lotfi
Mohammad Lotfi

Reputation: 400

Great, it works...

function reset_html(id)
{
    $('#'+id).html($('#'+id).html());
}

$(document).ready(function()
{
    var file_input_index = 0;
    $('input[type=file]').each(function()
    {
        file_input_index++;
        $(this).wrap('<div id="file_input_container_'+file_input_index+'"></div>');
        $(this).after('<input type="button" value="Clear" onclick="reset_html(\'file_input_container_'+file_input_index+'\')" />');
    });
});

Upvotes: 1

Related Questions