I am trying to dynamically change the width of the text box which displays the file name chosen in the file dialog since some of the filenames are quite long. The code shown below does not change the size of the textbox (SelectedFile) I tried using width property as well but to no avail. Something seems to be overriding my changes.
Given the following html
<div class="input-group">
<label class="input-group-btn">
<span class="btn btn-primary">
Browse… <input type="file" style="display: none;" >
</span>
</label>
<input id="SelectedFile" type="text" class="form-control" readonly >
</div>
And the following jQuery
<script type="text/javascript">
$(function () {
//attach the `fileselect` event to all file inputs on the page
$(document).on('change', ':file', function () {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});
// Watch for our custom `fileselect` event
$(document).ready(function () {
$(':file').on('fileselect', function (event, numFiles, label) {
var input = $(this).parents('.input-group').find(':text'),
log = numFiles > 1 ? numFiles + ' files selected' : label,
fileLength = (label.length * 8) + 50;
if (input.length) {
input.val(log);
} else {
if (log) alert(log);
}
var newWidth = fileLength.toString() + 'px;';
$('#SelectedFile').css({ maxWidth: newWidth });
});
});
});
</script>