Ciao a tutti, sto litigando con IE perchè non fa quello che dovrebbe con setAttribute, ma per me è fondamentale usare quel comando, dato che devo aggiungere e togliere campi dal form.
Ho provato delle alternative ma nn riesco a passargli poi come parametro il tr da cancellare e risistemare gli indici del form.
Ecco il codice completo.
Codice PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Aggiungi</title>
<script language="JavaScript">
var vnCountField=0;
function fnAggiungiField(){
var oTr = document.createElement("TR");
var oTd1 = document.createElement("TD");
var oTd2 = document.createElement("TD");
var oField = document.createElement("INPUT");
var oFieldCancel = document.createElement("INPUT");
oField.setAttribute("type","text");
oField.setAttribute("name","testo" + vnCountField);
oField.setAttribute("value",document.getElementById("osCategoria").value);
oField.setAttribute("disabled", true);
oFieldCancel.setAttribute("type", "button");
oFieldCancel.setAttribute("name", "cancel" + vnCountField);
oFieldCancel.setAttribute("value", "Cancella" + vnCountField);
oFieldCancel.setAttribute("onclick", "fnCancellaField(" + vnCountField + ")");
oTd1.appendChild(oField);
oTd2.appendChild(oFieldCancel);
oTr.appendChild(oTd1);
oTr.appendChild(oTd2);
document.getElementById('tabella').getElementsByTagName('TBODY')[0].appendChild(oTr);
vnCountField++
}
function fnCancellaField(pnCancellaField) {
var tb = document.getElementById('tabella').getElementsByTagName('TBODY')[0];
var selectedTr = tb.getElementsByTagName('TR')[pnCancellaField];
tb.removeChild(selectedTr);
vnCountField--
fnAggiornaIndiciTabella();
}
function fnAggiornaIndiciTabella() {
var tb = document.getElementById('tabella').getElementsByTagName('TBODY')[0];
for (var i = 0; i < vnCountField; i++) {
var otTr = tb.getElementsByTagName("TR")[i];
var otTd = otTr.getElementsByTagName("TD")[1];
var oFieldCancel = otTd.getElementsByTagName("INPUT")[0];
oFieldCancel.setAttribute("onClick", "fnCancellaField(" + i + ")");
oFieldCancel.setAttribute("value", "Cancella" + i);
oFieldCancel.setAttribute("name", "cancel" + i);
}
}
</script>
</head>
<body>
<form name="modulo">
<input type="button" value="Aggiungi" onclick="fnAggiungiField()">
<input type="text" id="osCategoria" name="osCategoria">
</p>
<div>
<table border="0" id="tabella">
<tbody>
</tbody>
</table>
</div>
</form>
</body>
</html>