1

I have a file upload field,that is,<s:file> </s:file>. And I have a button "clear". On clicking this button, the field which has some file link should become empty.Could anyone help me with this?

My code:

<s:file id="filetestplanid2" name="testPlanDto.testFile" label="test"
        tooltipIconPath="../../KY/images/common/buttons/uploadBtn.png"  
        title="Browse" tooltip="Browse..."  cssClass="file" />

My javascript:

$('#filetestplanid2').val(null);
$('#filetestplanid2').val("");

I tried these but no luck.

Also tried:

var file = $("#filetestplanid2"); file.replaceWith(file = file.clone(true));

$('#filetestplanid2').html( $('#filetestplanid2').html() );
Roman C
  • 49,761
  • 33
  • 66
  • 176
kishore
  • 1,587
  • 4
  • 15
  • 17

2 Answers2

1

Assuming

<input type="button" onclick="clearFileElement('filetestplanid2');" />

Vanilla JS

function clearFileElement(fileId){
    document.getElementById(fileId).value = '';
};

Demo: http://jsfiddle.net/2nxGr/

Can't make it works with jQuery but basically you have to substitute it with a clone of itself (with all the properties, gained using clone(true)).

Just stick to the plain JS version, it works like a charm.

EDIT

I've found now a very smart solution that works on every browser: https://stackoverflow.com/a/13351234/1654265

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • @kishore Then check your assumptions: inspect the element and see its ID. Check the JS console. Debug. – Dave Newton Jun 18 '13 at 13:07
  • 1
    The first solution works too, and IT IS an answer. I edited to add a better one I found, to help more users. Could you please stop downvoting my answers ? I mean like here: http://stackoverflow.com/a/17060676/1654265 – Andrea Ligios Jun 18 '13 at 13:52
0

You can make it empty using Jquery too, just like this:

$("#filetestplanid2")[0].value = ""

or

$("#filetestplanid2").get(0).value = ""
Muhammad Musavi
  • 2,512
  • 2
  • 22
  • 35