Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 File Input

I am struggling with bootstrap 4 file browser. If I use custom-file-control I will see Choose file value all the time. https://v4-alpha.getbootstrap.com/components/forms/#file-browser

I would like to change the value of choose file after the file has been chosen. This value is actually hidden in css .custom-file-control:lang(en)::after and I do not know how to access and change it in javascript. I can get the value of chosen file like this:

document.getElementById("exampleInputFile").value.split("\\").pop(); 

not I need to change

.custom-file-control:lang(en)::after {     content: "Choose file..."; } 

somehow

link: http://codepen.io/Matoo125/pen/LWobNp

like image 497
Matej Vrzala M4 Avatar asked Apr 06 '17 08:04

Matej Vrzala M4


People also ask

How do I select a file in Bootstrap?

Compose Bootstrap's custom file input with custom label. Hide default Choose file button with the ::file-selector-button pseudo-element. There are pseudo-elements ::-webkit-file-upload-button and ::-ms-browse for older versions of Chrome/Opera/Safari and Edge/IE respectively.

How do I customize Bootstrap input?

To create a custom file upload, wrap a container element with a class of . custom-file around the input with type="file". Then add the . custom-file-input to it.


2 Answers

Updated 2021

Bootstrap 5

Custom file input no longer exists so to change Choose file... you'd need to use JS or some CSS like this.

Bootstrap 4.4

Displaying the selected filename can also be done with plain JavaScript. Here's an example that assumes the standard custom-file-input with label that is the next sibling element to the input...

document.querySelector('.custom-file-input').addEventListener('change',function(e){   var fileName = document.getElementById("myInput").files[0].name;   var nextSibling = e.target.nextElementSibling   nextSibling.innerText = fileName }) 

https://codeply.com/p/LtpNZllird

Bootstrap 4.1+

Now in Bootstrap 4.1 the "Choose file..." placeholder text is set in the custom-file-label:

<div class="custom-file" id="customFile" lang="es">         <input type="file" class="custom-file-input" id="exampleInputFile" aria-describedby="fileHelp">         <label class="custom-file-label" for="exampleInputFile">            Select file...         </label> </div> 

Changing the "Browse" button text requires a little extra CSS or SASS. Also notice how language translation works using the lang="" attribute.

.custom-file-input ~ .custom-file-label::after {     content: "Button Text"; } 

https://codeply.com/go/gnVCj66Efp (CSS)
https://codeply.com/go/2Mo9OrokBQ (SASS)

Another Bootstrap 4.1 Option

Alternatively you can use this custom file input plugin

https://www.codeply.com/go/uGJOpHUd8L/file-input


Bootstrap 4 Alpha 6 (Original Answer)

I think there are 2 separate issues here..

<label class="custom-file" id="customFile">         <input type="file" class="custom-file-input">         <span class="custom-file-control form-control-file"></span> </label> 

1 - How the change the initial placeholder and button text

In Bootstrap 4, the initial placeholder value is set on the custom-file-control with a CSS pseudo ::after element based on the HTML language. The initial file button (which isn't really a button but looks like one) is set with a CSS pseudo ::before element. These values can be overridden with CSS..

#customFile .custom-file-control:lang(en)::after {   content: "Select file..."; }  #customFile .custom-file-control:lang(en)::before {   content: "Click me"; } 

2 - How to get the selected filename value, and update the input to show the value.

Once a file is selected, the value can be obtained using JavaScript/jQuery.

$('.custom-file-input').on('change',function(){     var fileName = $(this).val(); }) 

However, since the placeholder text for the input is a pseudo element, there's no easy way to manipulate this with Js/jQuery. You can however, have a another CSS class that hides the pseudo content once the file is selected...

.custom-file-control.selected:lang(en)::after {   content: "" !important; } 

Use jQuery to toggle the .selected class on the .custom-file-control once the file is selected. This will hide the initial placeholder value. Then put the filename value in the .form-control-file span...

$('.custom-file-input').on('change',function(){   var fileName = $(this).val();   $(this).next('.form-control-file').addClass("selected").html(fileName); }) 

You can then handle the file upload or re-selection as needed.

Demo on Codeply (alpha 6)

like image 166
Zim Avatar answered Sep 29 '22 04:09

Zim


I just solved it this way

Html:

<div class="custom-file">    <input id="logo" type="file" class="custom-file-input">    <label for="logo" class="custom-file-label text-truncate">Choose file...</label> </div> 

JS:

$('.custom-file-input').on('change', function() {     let fileName = $(this).val().split('\\').pop();     $(this).next('.custom-file-label').addClass("selected").html(fileName);  }); 

Note: Thanks to ajax333221 for mentioning the .text-truncate class that will hide the overflow within label if the selected file name is too long.

like image 40
Elnoor Avatar answered Sep 29 '22 05:09

Elnoor