What I am trying to do is to add a button for uploding a file, which i can style and which shows me the name of the chosen file.
I have searched a lot, and the more I search the more different sollutions I find. I found out, that you can't style the input directly, because you just cant... :(.
Finally I picked a little from every solution and came to the following:
I set up a span in a lable for the input[type file] and hid the input by display:none;.
Now I can style the span to my needs, and on click it calls the input. With the little JS at the end, take the value of the input (what is the name of the picked file) and show it insted of the span-text on the styled button.
but see yourself:
#input-file {
display: none;
}
.upload-btn {
display: block;
text-align: center;
margin: 20px auto 20px;
width: 50%;
height: 30px;
background-color: #fcff7f;
line-height: 30px;
}
.upload-btn:hover {
background-color: #c39d5a;
color: white;
cursor: pointer;
}
<label class="upload-btn">
<input type="file" id="input-file" onchange="changeText()" />
<span id="selectedFileName">Browse</span>
</label>
<script type="text/javascript">
function changeText() {
var y = document.getElementById("input-file").value;
document.getElementById("selectedFileName").innerHTML = y;
}
</script>
What I would like to know is, what is wrong with this? For my understanding it is very simple, so simple that i dont understand "yet" why all the solutions I found on the internet, are way more difficult. They allways try to use fake inputs, with opacity and so on.
So please can someone very experienced, tell me if I can use it like this, or do I have to change it? To what?
Thanks ;) and a lot of excuses for the bad english.