Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the button text of <input type="file" />?

Tags:

html

input

<input type="file" value="Browse" name="avatar" id="id_avatar" />

I tried to modify the value, but it's not working. How to customize the button text?

like image 257
user198729 Avatar asked Dec 22 '09 05:12

user198729


People also ask

Can we change Choose file button text?

It's basically impossible to change the text of the native file input button.


3 Answers

Use Bootstrap FileStyle, which is used to style the file fields of forms. It is a plugin for a jQuery-based component library called Twitter Bootstrap

Sample usage:

Include:

<script type="text/javascript" src="js/bootstrap-filestyle.min.js"> </script>

Via JavaScript:

$(":file").filestyle();

Via data attributes:

<input type="file" class="filestyle" data-classButton="btn btn-primary" data-input="false" data-classIcon="icon-plus" data-buttonText="Your label here.">
like image 150
Fernando Kosh Avatar answered Oct 11 '22 05:10

Fernando Kosh


You can put an image instead, and do it like this:

HTML:

<img src="/images/uploadButton.png" id="upfile1" style="cursor:pointer" />
<input type="file" id="file1"  name="file1" style="display:none" />

JQuery:

$("#upfile1").click(function () {
    $("#file1").trigger('click');
});

CAVEAT: In IE9 and IE10 if you trigger the onClick in a file input via javascript, the form will be flagged as 'dangerous' and cannot be submitted with javascript, not sure if it can be submitted traditionally.

like image 24
ParPar Avatar answered Oct 11 '22 05:10

ParPar


Hide the file input. Create a new input to capture a click event and forward it to the hidden input:

<input type="button" id="loadFileXml" value="loadXml" onclick="document.getElementById('file').click();" />
<input type="file" style="display:none;" id="file" name="file"/>
like image 130
MushyPeas Avatar answered Oct 11 '22 04:10

MushyPeas