I've only seen this problem on Safari and old versions of IE. Regarding the reason, I've never found anything documented on this matter.
At one point, jQuery itself was disabling changes on the type
attribute for an input
because it was causing problems in IE. It was documented as:
// We can't allow the type property to be changed (since it causes problems in IE)
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
throw "type property can't be changed";
You can find more informations regarding this issue in this question.
Nowadays, it seems to no longer be the case but Safari is still silently failing when changing the type to file
. On the other hand, changing it from file
works without any problem. Since this is not documented, I've always assumed it was for "security" measures but I've never found any concrete evidence.
The workaround I'm using is to detect when the new type will be file
and in this special case, create a new input
element, set its type to file, set its id and replace the existing input with the new one.
var input = $('#input');
$('button[id^=type-]').on('click', function() {
var type = $(this).attr('id').replace('type-', '');
if (type === 'file') {
var newElement = document.createElement('input');
newElement.type = type;
newElement.id = input.attr('id');
input.replaceWith(newElement);
input = $('#input');
} else {
input.attr('type', type);
}
$('#debug-info').html(
'Trying to change the input type to <strong>' + type + '</strong>.<br>' +
'Actual input type is <strong>' + input.attr('type') + '<strong>.'
);
if (type == input.attr('type')) {
$('#debug-info').css('color', 'green');
} else {
$('#debug-info').css('color', 'red');
}
});
You can check it in this fiddle.