5

I want to hide the file input "Choose file" button but I want to keep the filename. How can I do this?

enter image description here

<input
  name="upload"
  id="upload-input"
  placeholder="Select a file"
  ref="fileInput"
  type="file"
  required>
VXp
  • 10,960
  • 6
  • 31
  • 42
bigpotato
  • 24,574
  • 50
  • 160
  • 313
  • This isn't tagged javascript - is that acceptable? If so, this has been asked / answered: https://stackoverflow.com/questions/35819391/css-hide-choose-file-button-but-display-file-after-select – random_user_name Jan 31 '18 at 17:36

2 Answers2

2

You can save the file name to another html element and then hide the choose file input like this.

function getFileName()
{
var x = document.getElementById('upload-input')
x.style.visibility = 'collapse'
document.getElementById('fileName').innerHTML = x.value.split('\\').pop()
}
<input
  name="upload"
  id="upload-input"
  placeholder="Select a file"
  ref="fileInput"
  type="file"
  required 
  onchange="getFileName()"><span id="fileName"></span>
VXp
  • 10,960
  • 6
  • 31
  • 42
Scath
  • 3,744
  • 10
  • 29
  • 39
  • 1
    You can use x.value.split('\\').pop() for filename instead of path – JasonB Jan 31 '18 at 17:29
  • OP doesn't have the question tagged with javascript.... your solution requires javascript... might be worth clarifying if javascript is OK, and / or if it's possible / not possible with pure HTML / CSS. – random_user_name Jan 31 '18 at 17:35
  • Additionally, if JS is the answer, then this should be closed as duplicate (https://stackoverflow.com/questions/35819391/css-hide-choose-file-button-but-display-file-after-select) instead of posting a new answer – random_user_name Jan 31 '18 at 17:37
0

Add a new component at the place:

 <span id='upload-file-name'></span>

With JavaScript, to get the file name first

var em = document.getElementById("upload-input");
var fname = em.name;

Hide the file upload input field

em.style.display = 'none';

Put the file name to this new component

document.getElementById("upload-file-name").innerHTML = fname;
ild flue
  • 1,268
  • 6
  • 9
  • OP doesn't have the question tagged with javascript.... your solution requires javascript... might be worth clarifying if javascript is OK, and / or if it's possible / not possible with pure HTML / CSS. – random_user_name Jan 31 '18 at 17:35
  • Additionally, if JS is the answer, then this should be closed as duplicate (https://stackoverflow.com/questions/35819391/css-hide-choose-file-button-but-display-file-after-select) instead of posting a new answer – random_user_name Jan 31 '18 at 17:37
  • @cale_b The OP did not put limitation on any possibility. – ild flue Jan 31 '18 at 17:37
  • Absolutely OP did: the tags indicate the technologies that are desired. that's what tags are for. You wouldn't post a PHP answer to a question tagged Ruby.... – random_user_name Jan 31 '18 at 17:39