codice:
var num=1;
function accoda(){
if(document.createElement && document.getElementById && document.getElementsByTagName) {
// crea elementi
var oTr=document.createElement("TR");
oTr.setAttribute("class","product");
var oTd1=document.createElement("TD");
var oTd2=document.createElement("TD");
var oField=document.createElement("INPUT");
$(function() {
$(oField).autocomplete({
source: "cerca_oggetto.php",
});
});
var oFieldB =document.createTextNode(" ");
var oFieldC=document.createElement("INPUT");
var oFieldD =document.createTextNode(" ");
var oFieldE=document.createElement("INPUT");
var oFieldF =document.createTextNode(" \u20AC \u00A0 \u00A0");
var oFieldG=document.createElement("INPUT");
var oFieldH =document.createTextNode("\u00A0 \u00A0 Totale: ");
var oFieldI=document.createElement("INPUT");
var oFieldL =document.createTextNode(" \u20AC ");
var oButt=document.createElement("INPUT");
// setta attributi
oField.setAttribute("type","text");
oField.setAttribute("name","codice_"+num);
oField.setAttribute("id","codice_");
oField.setAttribute("placeholder","Nome Prodotto");
oField.setAttribute("size","30");
oField.setAttribute("class","codice");
oField.setAttribute("onchange", "myFunction(this)");
oFieldB.setAttribute("type","text");
oFieldB.setAttribute("name","un_misura"+num);
oFieldB.setAttribute("id","un_misura_");
oFieldB.setAttribute("placeholder","Un. Misura");
oFieldB.setAttribute("readonly","");
oFieldB.setAttribute("size","10");
oFieldB.setAttribute("class","un_misura");
oFieldB.setAttribute("style","text-align:center");
oFieldC.setAttribute("type","text");
oFieldC.setAttribute("name","prezzo_"+num);
oFieldC.setAttribute("id","prezzo_");
oFieldC.setAttribute("placeholder","Prezzo'");
oFieldC.setAttribute("size","8");
oFieldC.setAttribute("class","prezzo");
oFieldC.setAttribute("style","text-align:right");
oFieldD.setAttribute("type","text");
oFieldD.setAttribute("name","qta_"+num);
oFieldD.setAttribute("id","qta_");
oFieldD.setAttribute("placeholder","Quantita'");
oFieldD.setAttribute("size","8");
oFieldD.setAttribute("class","qta");
oFieldD.setAttribute("onkeyup", "calc(this)");
oFieldD.setAttribute("style","text-align:center");
oFieldE.setAttribute("type","text");
oFieldE.setAttribute("name","tot"+num);
oFieldE.setAttribute("id","tot_");
oFieldE.setAttribute("placeholder","Tot'");
oFieldE.setAttribute("size","8");
oFieldE.setAttribute("readonly","");
oFieldE.setAttribute("class","tot");
oFieldE.setAttribute("style","text-align:right");
oFieldE.setAttribute("onkeyup", "total(this)");
oButt.setAttribute("type","button");
oButt.setAttribute("class","btn btn-danger");
oButt.setAttribute("value","rimuovi");
// setta gestore evento
if(oButt.attachEvent) oButt. attachEvent('onclick',function(e){rimuovi(e);})
else if(oButt.addEventListener) oButt. addEventListener('click',function(e){rimuovi(e);},false)
// appendi al relativo padre
oTd1.appendChild(oField);
oTd1.appendChild(oFieldB);
oTd1.appendChild(oFieldC);
oTd1.appendChild(oFieldD);
oTd1.appendChild(oFieldE);
oTd2.appendChild(oButt);
oTr.appendChild(oTd1);
oTr.appendChild(oTd2);
document.getElementById('tabella').getElementsByTagName('TBODY')[0].appendChild(oTr);
var tabella =0;
$('tabella').each(function(){
tot_ricetta +=Number($(this).children('.tot').val());
});
alert (tot_ricetta);
$("#tot_ricetta").html(tot_ricetta);
document.getElementById('iterazioni_form_aggiuntivi').value = num;
// incrementa variabile globale
num++
}
}
function rimuovi(e){
if(document.removeChild && document.getElementById && document.getElementsByTagName) {
if(!e) e=window.event;
var srg=(e.target)?e.target:e.srcElement;
// risali al tr del td che contiene l' elemento che ha scatenato l' evento
while(srg.tagName!="TR"){srg=(srg.parentNode)?srg.parentNode:srg.parentElement}
// riferimento al tbody
var tb=document.getElementById('tabella').getElementsByTagName('TBODY')[0];
// rimuovi
tb.removeChild(srg);
}
}