How to Change the \'Choose File...\' Text in Bootstrap 4 File Input?

Barbara Streisand
Release: 2024-10-30 18:50:02
Original
759 people have browsed it

How to Change the

Bootstrap 4 File Input: Customizing 'Choose File'...

Custom file inputs in Bootstrap 4 have a placeholder text "Choose file..." that can be tricky to change. Let's explore two separate issues:

1. Modifying Initial Placeholder and Button Text

Bootstrap 4 sets the initial placeholder and button text through CSS pseudo elements based on the HTML language. To override these values, use custom CSS:

<code class="CSS">#customFile .custom-file-control:lang(en)::after {
  content: "Select file...";
}

#customFile .custom-file-control:lang(en)::before {
  content: "Click me";
}</code>
Copy after login

2. Displaying Selected File Name

To get the selected file name, use JavaScript/jQuery:

<code class="JavaScript">$('.custom-file-input').on('change', function() {
  var fileName = $(this).val();
});</code>
Copy after login

However, manipulating the input's placeholder text is not directly possible. Instead, use another CSS class to hide the initial placeholder once a file is selected and display the file name in a nearby element:

<code class="CSS">.custom-file-control.selected:lang(en)::after {
  content: "" !important;
}</code>
Copy after login
<code class="JavaScript">$('.custom-file-input').on('change', function() {
  var fileName = $(this).val();
  $(this).next('.form-control-file').addClass("selected").html(fileName);
});</code>
Copy after login

This will hide the default "Choose file..." text and display the selected file name. You can then handle further actions like file upload or re-selection as needed.

The above is the detailed content of How to Change the \'Choose File...\' Text in Bootstrap 4 File Input?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template