Salva a tutti non riesco proprio a capire cosa sto sbagliando.
Ho degli array popolati "dinamicamente" in base alla scelta fatte dall'utente.
Voglio stampare i valori di questi array in formato tabellare.
Ho usato questo codice:
function stampa_tabella() {
var tab_ris = document.getElementById("tab_risultati");
var table = document.createElement("table");
var tbody = document.createElement("tbody");
table.appendChild(tbody);
var row = document.createElement("tr");
var cell = document.createElement("th");
cell.innerHTML = "tab_risultati";
row.appendChild(cell);
tbody.appendChild(row)
}
function costruisciTabella(a,b)
{
document.write(gamma_utente);
var DATI_TAB = [
5,
"pippo"
];
var tab_ris = window.document.getElementById("tab_risultati");
alert("tab_ris: "+ tab_ris);
var tabella = document.createElement("table");
tabella.border="1";
var tbody = document.createElement("tbody");
tabella.appendChild(tbody);
for (var r=0; r<DATI_TAB.length; r++)
{
var riga = tabella.insertRow(-1);
for (var c=0; c<(DATI_TAB[r]).length; c++)
{
var cella = riga.insertCell(c);
cella.style.padding = "4px";
cella.appendChild(document.createTextNode(DATI_TAB[r][c]));
}
}
tab_ris.appendChild(tabella);
}
Se chiamo la funzione direttamente dalla pagina "prova.html" in questo modo
<script type="text/javascript">costruisciTabella();</script>
tutto funziona a dovere MA se richiamo la funzione all'evento ONCLICK di un button ho il seguente errore "tab_ris is null"
PErche? cosa sbaglio??![]()
![]()
Grazie a tutti in anticipo