0

I use this solution to change the button text of <input type="file" />. It works, but it have a small drawback - after a file was chosen the user will not see what file was selected, because this info is not displayed (it is hidden with the original button). How to make visible this information without to display the original (hidden) button?

The code:

<input type="button" id="loadFileXml" value="loadXml" onclick="document.getElementById('file').click();" />
<input type="file" style="display:none;" id="file" name="file"/>
Yurié
  • 2,148
  • 3
  • 23
  • 40
  • You could create a div and populate it with info from the `files` property in the input: https://stackoverflow.com/questions/26082721/how-to-display-selected-file-names-before-uploading-multiple-files-in-struts2 – yuriy636 Nov 18 '17 at 22:55
  • @lurie check the answer. let me know if you expected anything else – Muhammad Usman Nov 18 '17 at 23:22

2 Answers2

1

You can create a span with no text in it and then update its value to the value of your input type of file. Something like

function showFileName(){
document.getElementById("fileName").innerHTML = document.getElementById("file").files[0].name

}
<input type="button" id="loadFileXml" value="loadXml" onclick="document.getElementById('file').click();" />
<span id="fileName"></span>
<input type="file" onChange="showFileName()" style="display:none;" id="file" name="file"/>
Muhammad Usman
  • 10,039
  • 22
  • 39
1

You can add a placeholder element under your visible input:

<p id="file_path"></p>

Then (I used jQuery for this part), events side:

​$('#file').change
(
    function(e)
    {
        $('#file_path').text(e.target.files[0]);
    }
);​​​
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98