Get a free trial // Grow your CSS skills // Land your dream job

Clear a File Input

Last updated on:

You can just clone it and replace it with itself, with all events still attached.

var input = $("#control");

function something_happens() {



  1. Sreekanth
    Permalink to comment#

    Click on Clear button for more than 1 time does n’t work.

  2. Karan Menon
    Permalink to comment#

    Not working…

  3. Tobias
    Permalink to comment#

    Same here, the example works only the first time.
    Selecting a different file puts “changed” to the console.log but clicking on the “clear” link does nothing.
    Win7/Chrome 26

  4. dibiler
    Permalink to comment#

    I believe that it’s not working,because variable “controlBn” is assigned only once (the first time equals $(‘#control’). And as that element is being replaced with a new clone of it, the var “controlBn” may be pointing to an inexistent element after that.
    I would put just :
    $('#clear').on('click' ,function(){
    $('#control').replaceWith( $('#control').val('').clone( true ) );

    rather than the code above
    (just a guess)

  5. niwtsew
    Permalink to comment#

    Though .val(”) works for FF, but it does not work for IE.

  6. Trevor
    Permalink to comment#

    Thank you. Just what I needed. Works perfectly ;)

  7. Jesin
    Permalink to comment#

    @dibiler’s solution works great. The control variable has to be reinitialized inside the click callback function.

    clearBn.on("click", function(){
        var control = $("#control");
        control.replaceWith( control.val('').clone( true ) );

    A working demo is available here –

  8. Pablo
    Permalink to comment#


  9. Ibrahim Islam
    Permalink to comment#


  10. ETorres
    Permalink to comment#

    Hey … it’s great! but not work to FILE type :-(

  11. Muhamad Risman
    Permalink to comment#

    thank you, very helpful for me :)

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed