Material Design File Upload Input

Material Design File Upload Input
Code Snippet:Material Design File Input
Author: Akhmed Gaziev
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 872
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a Material Design-inspired file upload input. The JavaScript functions dynamically update the displayed file name and adjust the input’s visual state based on user interaction. Helpful for enhancing file upload forms with a modern look and feel.

How to Create Material Design File Upload Input

1. First of all, load the Material CSS and Material Icons by adding the following CDN links into the head tag of your HTML document.

<link rel='stylesheet' href='https://storage.googleapis.com/code.getmdl.io/1.0.2/material.blue_grey-red.min.css'>
<link rel='stylesheet' href='https://fonts.googleapis.com/icon?family=Material+Icons'>

2. Create the necessary HTML structure for the file upload input. Ensure you include the following elements:

<div class="file_input_div">
    <div class="file_input">
      <label class="image_input_button mdl-button mdl-js-button mdl-button--fab mdl-button--mini-fab mdl-js-ripple-effect mdl-button--colored">
        <i class="material-icons">file_upload</i>
        <input id="file_input_file" class="none" type="file" />
      </label>
    </div>
    <div id="file_input_text_div" class="mdl-textfield mdl-js-textfield textfield-demo">
      <input class="file_input_text mdl-textfield__input" type="text" disabled readonly id="file_input_text" />
      <label class="mdl-textfield__label" for="file_input_text"></label>
    </div>
  </div>

3. Copy the CSS code into your stylesheet to ensure the proper styling for the file upload input. Customize the width and height of the file input by adjusting the values in the CSS class .file_input_div.

body {
  display: flex;
}

.file_input_div {
  margin: auto;
  width: 250px;
  height: 40px;
}

.file_input {
  float: left;
}

#file_input_text_div {
  width: 200px;
  margin-top: -8px;
  margin-left: 5px;
}

.none {
  display: none;
}

4. Now, load the Material JS by adding the following script before closing the body tag:

<script src='https://storage.googleapis.com/code.getmdl.io/1.0.0/material.min.js'></script>

5. Finally, add the JavaScript code within your HTML file or link it externally. Make sure to include it after the HTML content.

var fileInputTextDiv = document.getElementById('file_input_text_div');
var fileInput = document.getElementById('file_input_file');
var fileInputText = document.getElementById('file_input_text');

fileInput.addEventListener('change', changeInputText);
fileInput.addEventListener('change', changeState);

function changeInputText() {
  var str = fileInput.value;
  var i;
  if (str.lastIndexOf('\\')) {
    i = str.lastIndexOf('\\') + 1;
  } else if (str.lastIndexOf('/')) {
    i = str.lastIndexOf('/') + 1;
  }
  fileInputText.value = str.slice(i, str.length);
}

function changeState() {
  if (fileInputText.value.length != 0) {
    if (!fileInputTextDiv.classList.contains("is-focused")) {
      fileInputTextDiv.classList.add('is-focused');
    }
  } else {
    if (fileInputTextDiv.classList.contains("is-focused")) {
      fileInputTextDiv.classList.remove('is-focused');
    }
  }
}

Now you have a stylish file upload input ready to elevate the aesthetic and functionality of your forms. Integrate it effortlessly and enjoy a more user-friendly file upload experience.

That’s all! hopefully, you have successfully created a File Upload Input on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X