How can I change the default name "Select file" to "Select document" without using libraries?

Viewed 70

I want its structure to remain unchanged, which is still the button and the label that is where the file name is displayed, I want to be able to change the name of the button.

    <input class="form-control" type="file" value="Seleccionar documento">

2 Answers

Use a label:

div{
  position:relative;
}
input{
  display:none;
}
<div>
<input class="form-control" type="file" value="Seleccionar documento" id="upload">
<label for="upload"><button onclick="this.parentElement.previousElementSibling.click()">Choose File</button> Select document</label>
</div>

To show file names, we can use JS to add a change event listener to the input field that updates the label:

upload.addEventListener("change", function(){
  document.querySelector('label span').innerText = this.files[0].name;
})
div{
  position:relative;
}
input{
  display:none;
}
<div>
<input class="form-control" type="file" value="Seleccionar documento" id="upload">
<label for="upload"><button onclick="this.parentElement.previousElementSibling.click()">Choose File</button> <span>Select document</span></label>
</div>

I think its already been answered try looking at this link : Change default text in input type="file"?

Sample picked from the same source which worked for me:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <button style="display:block;width:120px; height:30px;" onclick="document.getElementById('getFile').click()">Your text here</button>
  <input type='file' id="getFile" style="display:none">
</body>

</html>

Related