jueves, 31 de enero de 2008

Crear dinamicamente inputs input en un formulario con Javascript

<script>

//***** variable global
var i=1;

function fEscribeDir()
{
addFormField(document.formu1,'file','fichero'+i);
document.formu1.numficheros.value=i;
i=i+1;
}


function addFormField (form, fieldType, fieldName, fieldValue)
{
if (document.getElementById)
{
var input = document.createElement('INPUT');
var div = document.createElement('DIV');

if (document.all)
{ // what follows should work
// with NN6 but doesn't in M14
input.type = fieldType;
input.name = fieldName;
input.value = fieldValue;
}
else if (document.getElementById)
{ // so here is the
// NN6 workaround
input.setAttribute('type', fieldType);
input.setAttribute('name', fieldName);
input.setAttribute('value', fieldValue);
}

input.style.width="320px";
div.appendChild(input);
document.getElementById('ficheros').appendChild(div);
}
}

</script>



<form name="FrmUpload">

<div id="ficheros" style="position: absolute; top: <?echo ($top+($salto*7));?>px; left: 40px; width: 340px; height: 90px; overflow: auto;">
<input type="button" name="boton" id="boton" value="Añadir otro fichero" onclick="javascript: fEscribeDir();">
<br>
<input type="file" name="fichero0" style="width: 320px;">

</div>

</form>

1 comentario:

Anónimo dijo...

huy me ayudo resto tu codigo muhcas gracias

Juan Ricardo