I was 100% sure that this code works, but in one special case it fails - took an hour to find the error (could not reproduce it on JSFiddle). I finally found the reason, but I do not know why this is happening. This only happens when I hide the input files and the result: which file I am changing, the first shortcut is selected and changed, look:
var activateFileSelection = function( container ) {
var container = container || $('body');
container.find(':file').each(function(i) {
var thisInput = $(this);
var thisLabel = thisInput.siblings('label');
if (thisLabel.length > 0 && !thisLabel.hasClass('file-input-label')) {
var thisLabelDefaultText = thisLabel.html();
thisLabel.addClass('file-input-label');
thisInput.on('change', function(e) {
if (thisInput.val()) {
thisLabel.html(thisInput.val());
} else {
thisLabel.html(thisLabelDefaultText);
};
});
};
});
};
activateFileSelection();
input {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label for="myfile">select 1</label>
<input id="myfile" type="file" />
</div>
<div>
<label for="myfile">select 2</label>
<input id="myfile" type="file" />
</div>
<div>
<label for="myfile">select 3</label>
<input id="myfile" type="file" />
</div>
Run codeAlso on JSFiddle .
If the input is not hidden, everything works fine: fiddle .
nevermind, , - , script . , , .