-
Switch type input
Salve a tutti, allora, io vorrei cambiare quando si preme su un button, il type di un input cambia;
esempio: da text a file
se invece si ripreme il bottone, succederà l'inverso, ovvero:
esempio: da file a text.
Dovrebbe essere facile da fare, io pensavo che bastasse una semplice funzione la quale conteneva delle condizioni e tramite quelle cambiavo il type dell'input.
Ovviamente intendo fare questo, utilizzando solo alcuni browser, come firefox o Chrome, me ne frega nulla di Explorer dato che è una cosa mia, che non voglio lasciar fare ai visitatori di un sito.
Codice HTML:
<script>
function cambia(x)
{
switch(x)
{
case 'file':
document.getElementById('campo').type = 'text';
break;
case 'text':
document.getElementById('campo').type = 'file';
break;
}
}
</script>
<input type='file' id='campo'> <button onClick='cambia(campo.type)'>Cambia</button>
Come è che potrei fare?
-
Questo:
Codice HTML:
<script>
function cambia(id)
{
switch(document.getElementById(id).type)
{
case 'file':
document.getElementById(id).type = 'text';
break;
case 'text':
document.getElementById(id).type = 'file';
break;
}
}
</script>
<input type='file' id='campo'> <button onClick='cambia("campo")'>Cambia</button>
Funziona solo su Opera.
Su Chromium passa solo da 'file' -> 'text', e non fa il viceversa.
Firefox, invece, non ne vuol sapere :???:.
Prova invece che con il getElementByIf() con il forms.... Prova a vedere qui...
Ciao!
-
-