GithubHelp home page GithubHelp logo

vvye / file-input Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 15 KB

A simple wrapper around HTML file inputs, for easier styling

Home Page: https://eric-kaiser.net/project/file-input-js

License: Do What The F*ck You Want To Public License

JavaScript 54.79% HTML 45.21%
wrapper fileinput file-upload

file-input's Introduction

file-input.js

This is a simple wrapper around the HTML <input type="file"> element, no more and not much less.
The input element is notoriously hard to style with CSS (and inconsistent in its appearance across browsers). Here's a JavaScript function that turns it into a "browse" button and a file name display instead, which can be targeted by CSS selectors no problem.

If you have JavaScript disabled, nothing will happen. (But nothing will break either!)

This is nothing fancy at all. If you're looking for a feature-rich file upload widget and don't mind using a library, try something like jQuery File Upload.

Usage

Basic

As simple as this:

fileInput();

will turn all input elements into their custom counterparts. The new element keeps the old one's CSS classes.

Advanced

To affect only specific input elements, you can pass a CSS selector or an HTMLElement object:

fileInput('.foo > .bar');
fileInput(document.getElementById('baz'));

Styling

The new file input has an a element for the "browse" button and a span for the file name box. You can target them with CSS selectors as usual. To give them class names, use the buttonClasses and fileNameClasses options explained below.

Options

You can specify additional options by passing an object as the second parameter. The following keys will have an effect:

  • buttonText: what the "browse" button should say (default: "Browse…").
  • noFileText: what the file name text should say when no file is selected (default: "No file selected.").
  • multiFilesText: what the file name text should say when multiple files are selected (default: "{num} files selected"). Use {num} as a placeholder for the number of files.
  • noFilesText: what the file name text should say when no files are selected, but multiple could be (default: "No files selected").
  • buttonClasses: what CSS classes the "browse" button should have (array of strings; default: []).
  • fileNameClasses: what CSS classes the file name text should have (array of strings; default: []).

Examples

See sample.html for examples.

Feedback?

Found a bug or got any feedback? Let me know! Open an issue or something. I'm still new to this.

file-input's People

Contributors

vvye avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.