Pretty file input field in Bootstrap

Jackie Martinez (#31103)

Twitter Bootstrap is great, but there are places where it just stops short. One of those is the file input field, where the guys at bootstrap (and probably rightfully so) – just
left it as the browser default.
The problem is – it makes your pretty bootstrap forms ugly.
No worries – the following can help:

Ninja edit: Look at the comments – there are some browser specific enhancements.
Ninja edit 2: Look at jasny's fork for file-upload integrated into bootsrap and many more goodies!!

The code

<input id="lefile" type="file" style="display:none">
<div class="input-append">
   <input id="photoCover" class="input-large" type="text">
   <a class="btn" onclick="$('input[id=lefile]').click();">Browse</a>
</div>

<script type="text/javascript">
$('input[id=lefile]').change(function() {
   $('#photoCover').val($(this).val());
});
</script>

So this is quite easy:

  1. Define a normal, hidden file input field, and a visible bootstrap-styled field that looks pretty.
  2. Attach an onclick to the pretty input, that makes it click the hidden one.
  3. When the pretty Browse button is clicked – it sends a click to the real file input. This will in turn pop up the file selection dialog.
  4. After the user selects a file, the hidden file input’s change() event is called. This will copy the value of the real (hidden) file
    input to the pretty one. We do this to mimic the behaviour of file input fields – that they show you the file name after it is selected.
  5. Voila!
Pretty file input with Twitter Bootstrap

Pretty file input with Twitter Bootstrap

53 thoughts on “Pretty file input field in Bootstrap

  1. Pingback: Jussie | Best Bootstrap Resources

  2. Pingback: iJussie | Best Bootstrap Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>