Search code examples
jqueryclonefile-io

Cloning fieldset with jQuery without keeping value for input type file


How can I remove the value for an input of type file which is child of a fieldset that gets cloned with jQuery.

If the cloned object has a value already set for the file input, it will keep it.

Thank you.


Solution

  • Updated:

    Here's a new approach using some of the tips from redsquare and Šime:

    var $clone = $('#my-fieldset').clone(),
        $clonedFileInputs = $clone.find('input:file');
    
    // this browser check is nasty, but I don't know how to feature-detect this...
    if ($.browser.msie) {
        // avoid security error by replacing input, rather than setting value
        $clonedFileInputs.replaceWith(function() {
            return $(this).clone();
        });
    } else {
        $clonedFileInputs.val('');
    }
    
    $clone.appendTo('#new-parent');