hello,
i have a table that creates and deletes rows dynamically using
createElement, appendChild, removeChild.
when i have added the required amount of rows and input my data, i
would like to calculate the totals in each row.
when i try however, i receive the error:
"Error: 'elements[...]' is null or not an object"
i have tried looping through all elements in the form and this gives
me the name & value of each element.
when i call the element by name however, i receive the above error.
it is easier to explain by cuting and pasting the source of my html
file below:
i'd greatly appreciate any pointers, here's the code.
<script language="JavaS cript" type="text/JavaScript" >
<!--
var InternetExplore r = navigator.appNa me.indexOf("Mic rosoft") != -1;
var globalChanged = false;
var globalNewRowCha nged = false;
var globalClaimLnsR owNum = 1;
function ex_amount(s_cur ){
switch (s_cur)
{
case '1':
return '1.2';
break;
case '2':
return '0.6';
break;
case '3':
return '1.5';
break;
case '4':
return '2';
break;
case '5':
return '1';
break;
default:
return 0;
}
}
function detail_row(s_go ods_id,s_unit_p rice,s_currency _id,s_quantity, s_uplift,s_tota l_line,s_incl_u plift,s_incl_up lift_euro)
{
var body=document.b ody;
var Table,theRow,aC ell,aTextBox,ta blebody2,rowSec 2;
var theTable;
var rowSec1,current text;
var opt1,sel2;
var cellSec1,cellSe c2,cellSec3,cel lSec4,cellSec5;
var cellSec6,cellSe c7,cellSec8,cel lSec9,cellSec10 ;
theTable=docume nt.getElementBy Id('formation') .getElementsByT agName('tbody')[0];
tablebody2 = document.create Element('TBODY' );
rowSec2=documen t.createElement ('TR');
cellSec2=docume nt.createElemen t('TD');
sel2=document.c reateElement('s elect');
sel2.name='2d_c mb_goods__' + globalClaimLnsR owNum;
opt1=document.c reateElement('o ption');
opt1.value='9';
if (opt1.value==s_ goods_id){opt1. setAttribute('s elected',
true);}
opt1.innerHTML= 'Airconditioner s';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='5';
if (opt1.value==s_ goods_id){opt1. setAttribute('s elected',
true);}
opt1.innerHTML= 'Audio Goods';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='13' ;
if (opt1.value==s_ goods_id){opt1. setAttribute('s elected',
true);}
opt1.innerHTML= 'Video Recorder';
sel2.appendChil d(opt1);
cellSec2.append Child(sel2);
rowSec2.appendC hild(cellSec2);
cellSec3=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_unit_price );
aTextBox.size = '10';
aTextBox.name = '2d_txt_unit_pr ice__' + globalClaimLnsR owNum;
cellSec3.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec3);
cellSec4=docume nt.createElemen t('TD');
sel2=document.c reateElement('s elect');
sel2.name='2d_c mb_currency__' + globalClaimLnsR owNum;
opt1=document.c reateElement('o ption');
opt1.value='1';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'US$';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='2';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= '」STG';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='3';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'A$';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='4';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'NZ$';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='5';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'BHAT';
sel2.appendChil d(opt1);
cellSec4.append Child(sel2);
rowSec2.appendC hild(cellSec4);
cellSec5=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_quantity);
aTextBox.size = '5';
aTextBox.name = '2d_txt_quantit y__' + globalClaimLnsR owNum;
cellSec5.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec5);
cellSec6=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_total_line );
aTextBox.size = '5';
aTextBox.name = '2d_txt_total_l ine__' + globalClaimLnsR owNum;
cellSec6.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec6);
cellSec7=docume nt.createElemen t('TD');
sel2=document.c reateElement('s elect');
sel2.name='2d_c mb_uplift__' + globalClaimLnsR owNum;
opt1=document.c reateElement('o ption');
opt1.value='100 ';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= '100%';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='105 ';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= '105%';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='110 ';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= '110%';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='0';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= 'other';
sel2.appendChil d(opt1);
cellSec7.append Child(sel2);
rowSec2.appendC hild(cellSec7);
cellSec8=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_incl_uplif t);
aTextBox.size = '3';
aTextBox.name = '2d_txt_incl_up lift__' + globalClaimLnsR owNum;
cellSec8.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec8);
cellSec9=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_incl_uplif t_euro);
aTextBox.name = '2d_txt_incl_up lift_euro__' +
globalClaimLnsR owNum;
aTextBox.size = '5';
aTextBox.onchan ge=function(){c alculateTab(glo balClaimLnsRowN um);}
cellSec9.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec9);
cellSec10=docum ent.createEleme nt('TD');
ButtonSupprimer =document.creat eElement('input ');
ButtonSupprimer .type = 'button';
ButtonSupprimer .value = 'remove';
ButtonSupprimer .onclick=functi on(){formation. removeChild(tab lebody2)}
cellSec10.appen dChild(ButtonSu pprimer);
rowSec2.appendC hild(cellSec10) ;
tablebody2.appe ndChild(rowSec2 );
formation.appen dChild(tablebod y2);
globalClaimLnsR owNum = globalClaimLnsR owNum + 1;
}
function calculateTab(n_ line_no){
var oForm = document.forms[0];
alert('line number = ' + n_line_no);
oForm.elements['2d_txt_total_l ine__' + n_line_no].value =
nz(oForm.elemen ts['2d_txt_unit_pr ice__' + n_line_no].value) *
nz(oForm.elemen ts['2d_txt_quantit y__' + n_line_no].value);
}
function nz(s_val){
var s_datatype = "undefined" ;
if(s_val==null || s_val=='' || isNaN(s_val) ||
typeof(s_val)== s_datatype){
//return isNaN(num)?0:nu m;
return 0;
}
else{
//alert(s_val);
return parseFloat(s_va l);
}
}
function resetForm(){
var oForm = document.forms[0];
for (i=0; i<oForm.element s.length; i++){
oForm.elements[i].value = "";
}
}
function setLabel(s_mess age,s_label_nam e) {
document.all(s_ label_name).inn erText = s_message;
}
//-->
</script>
<html>
<head>
</head>
<body>
<form method="post" action="" id="form1" name="form1">
<table>
<tr>
<td width='4%'>
<tr>
<td colspan='6'> </td>
</tr>
<tr>
<td> </td>
<td><input type="button" name="Button2" value="change label"
onclick=setlabe l('here is some different text','label_de tail');>
</td>
<td><input type="button" name="Button" value="calculat e"
onclick=calcula teTab('1');></td>
<td colspan='1' align='right'>& nbsp;</td>
<td>
<input name='cmd_detai l_new' type='button' id='cmd_detail_ new'
value='add row [click here]' onclick=detail_ row();>
</td>
<td> </td>
<td colspan='3' align='right'>t otal </td>
<td>
<input name='2d_txt_de tail_total' type='text'
id='2d_txt_deta il_total' value='' size='10'
onChange=docume nt.forms[0].elements['txt_haschanged '].value ='yes';>
</td>
<td width='16%'>=EU R</td>
<td width='30%'>
<input name='2d_txt_de tail_total_euro ' type='text'
id='2d_txt_deta il_total_euro' value='' size='10'
onChange=docume nt.forms[0].elements['txt_haschanged '].value
='yes';></td>
</td>
</tr>
<tr>
<td colspan='6'></td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td height='20' valign='top'>
<table width='100%' border='1' cellpadding='0' cellspacing='0'
valign='top' id='formation'>
<tr>
<td width='15%'>&nb sp;<b>GOODS</b></td>
<td width='5%'>&nbs p;<b>UNIT PRICE</b></td>
<td width='10%'>&nb sp;<b>CURRENCY</b></td>
<td width='5%'>&nbs p;<b>QUANTITY</b></td>
<td width='5%'>&nbs p;<b>TOTAL LINE</b></td>
<td width='5%'>&nbs p;<b>UPLIFT (%)</b></td>
<td width='5%'>&nbs p;<b>INCL. UPLIFT</b></td>
<td width='5%'>&nbs p;<b>=EUR</b></td>
<td width='5%'>&nbs p;<b> </b></td>
</tr>
<tr>
<td colspan='10'>
<label id='label_detai l'>NO RECORDS FOUND</label>
</td>
</tr>
<input type='text' id='2d_txt_deta il_ids'
name='2d_txt_de tail_ids' value=''>
<input type='text' id='txt_update_ ids' name='txt_updat e_ids'
value=''>
</table> </td>
</tr>
</table>
</form>
</body>
</html>
thanks for any help, kie
p.s. the funtion setLabel that addresses: <label id='label_detai l'>
fails for a reason unknown to me, any pointers on this too would be
greatly appreciated.
i have a table that creates and deletes rows dynamically using
createElement, appendChild, removeChild.
when i have added the required amount of rows and input my data, i
would like to calculate the totals in each row.
when i try however, i receive the error:
"Error: 'elements[...]' is null or not an object"
i have tried looping through all elements in the form and this gives
me the name & value of each element.
when i call the element by name however, i receive the above error.
it is easier to explain by cuting and pasting the source of my html
file below:
i'd greatly appreciate any pointers, here's the code.
<script language="JavaS cript" type="text/JavaScript" >
<!--
var InternetExplore r = navigator.appNa me.indexOf("Mic rosoft") != -1;
var globalChanged = false;
var globalNewRowCha nged = false;
var globalClaimLnsR owNum = 1;
function ex_amount(s_cur ){
switch (s_cur)
{
case '1':
return '1.2';
break;
case '2':
return '0.6';
break;
case '3':
return '1.5';
break;
case '4':
return '2';
break;
case '5':
return '1';
break;
default:
return 0;
}
}
function detail_row(s_go ods_id,s_unit_p rice,s_currency _id,s_quantity, s_uplift,s_tota l_line,s_incl_u plift,s_incl_up lift_euro)
{
var body=document.b ody;
var Table,theRow,aC ell,aTextBox,ta blebody2,rowSec 2;
var theTable;
var rowSec1,current text;
var opt1,sel2;
var cellSec1,cellSe c2,cellSec3,cel lSec4,cellSec5;
var cellSec6,cellSe c7,cellSec8,cel lSec9,cellSec10 ;
theTable=docume nt.getElementBy Id('formation') .getElementsByT agName('tbody')[0];
tablebody2 = document.create Element('TBODY' );
rowSec2=documen t.createElement ('TR');
cellSec2=docume nt.createElemen t('TD');
sel2=document.c reateElement('s elect');
sel2.name='2d_c mb_goods__' + globalClaimLnsR owNum;
opt1=document.c reateElement('o ption');
opt1.value='9';
if (opt1.value==s_ goods_id){opt1. setAttribute('s elected',
true);}
opt1.innerHTML= 'Airconditioner s';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='5';
if (opt1.value==s_ goods_id){opt1. setAttribute('s elected',
true);}
opt1.innerHTML= 'Audio Goods';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='13' ;
if (opt1.value==s_ goods_id){opt1. setAttribute('s elected',
true);}
opt1.innerHTML= 'Video Recorder';
sel2.appendChil d(opt1);
cellSec2.append Child(sel2);
rowSec2.appendC hild(cellSec2);
cellSec3=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_unit_price );
aTextBox.size = '10';
aTextBox.name = '2d_txt_unit_pr ice__' + globalClaimLnsR owNum;
cellSec3.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec3);
cellSec4=docume nt.createElemen t('TD');
sel2=document.c reateElement('s elect');
sel2.name='2d_c mb_currency__' + globalClaimLnsR owNum;
opt1=document.c reateElement('o ption');
opt1.value='1';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'US$';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='2';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= '」STG';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='3';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'A$';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='4';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'NZ$';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='5';
if (opt1.value==s_ currency_id){op t1.setAttribute ('selected',
true);}
opt1.innerHTML= 'BHAT';
sel2.appendChil d(opt1);
cellSec4.append Child(sel2);
rowSec2.appendC hild(cellSec4);
cellSec5=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_quantity);
aTextBox.size = '5';
aTextBox.name = '2d_txt_quantit y__' + globalClaimLnsR owNum;
cellSec5.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec5);
cellSec6=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_total_line );
aTextBox.size = '5';
aTextBox.name = '2d_txt_total_l ine__' + globalClaimLnsR owNum;
cellSec6.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec6);
cellSec7=docume nt.createElemen t('TD');
sel2=document.c reateElement('s elect');
sel2.name='2d_c mb_uplift__' + globalClaimLnsR owNum;
opt1=document.c reateElement('o ption');
opt1.value='100 ';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= '100%';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='105 ';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= '105%';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='110 ';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= '110%';
sel2.appendChil d(opt1);
opt1=document.c reateElement('o ption');
opt1.value='0';
if (opt1.value==s_ uplift){opt1.se tAttribute('sel ected', true);}
opt1.innerHTML= 'other';
sel2.appendChil d(opt1);
cellSec7.append Child(sel2);
rowSec2.appendC hild(cellSec7);
cellSec8=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_incl_uplif t);
aTextBox.size = '3';
aTextBox.name = '2d_txt_incl_up lift__' + globalClaimLnsR owNum;
cellSec8.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec8);
cellSec9=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = nz(s_incl_uplif t_euro);
aTextBox.name = '2d_txt_incl_up lift_euro__' +
globalClaimLnsR owNum;
aTextBox.size = '5';
aTextBox.onchan ge=function(){c alculateTab(glo balClaimLnsRowN um);}
cellSec9.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec9);
cellSec10=docum ent.createEleme nt('TD');
ButtonSupprimer =document.creat eElement('input ');
ButtonSupprimer .type = 'button';
ButtonSupprimer .value = 'remove';
ButtonSupprimer .onclick=functi on(){formation. removeChild(tab lebody2)}
cellSec10.appen dChild(ButtonSu pprimer);
rowSec2.appendC hild(cellSec10) ;
tablebody2.appe ndChild(rowSec2 );
formation.appen dChild(tablebod y2);
globalClaimLnsR owNum = globalClaimLnsR owNum + 1;
}
function calculateTab(n_ line_no){
var oForm = document.forms[0];
alert('line number = ' + n_line_no);
oForm.elements['2d_txt_total_l ine__' + n_line_no].value =
nz(oForm.elemen ts['2d_txt_unit_pr ice__' + n_line_no].value) *
nz(oForm.elemen ts['2d_txt_quantit y__' + n_line_no].value);
}
function nz(s_val){
var s_datatype = "undefined" ;
if(s_val==null || s_val=='' || isNaN(s_val) ||
typeof(s_val)== s_datatype){
//return isNaN(num)?0:nu m;
return 0;
}
else{
//alert(s_val);
return parseFloat(s_va l);
}
}
function resetForm(){
var oForm = document.forms[0];
for (i=0; i<oForm.element s.length; i++){
oForm.elements[i].value = "";
}
}
function setLabel(s_mess age,s_label_nam e) {
document.all(s_ label_name).inn erText = s_message;
}
//-->
</script>
<html>
<head>
</head>
<body>
<form method="post" action="" id="form1" name="form1">
<table>
<tr>
<td width='4%'>
<tr>
<td colspan='6'> </td>
</tr>
<tr>
<td> </td>
<td><input type="button" name="Button2" value="change label"
onclick=setlabe l('here is some different text','label_de tail');>
</td>
<td><input type="button" name="Button" value="calculat e"
onclick=calcula teTab('1');></td>
<td colspan='1' align='right'>& nbsp;</td>
<td>
<input name='cmd_detai l_new' type='button' id='cmd_detail_ new'
value='add row [click here]' onclick=detail_ row();>
</td>
<td> </td>
<td colspan='3' align='right'>t otal </td>
<td>
<input name='2d_txt_de tail_total' type='text'
id='2d_txt_deta il_total' value='' size='10'
onChange=docume nt.forms[0].elements['txt_haschanged '].value ='yes';>
</td>
<td width='16%'>=EU R</td>
<td width='30%'>
<input name='2d_txt_de tail_total_euro ' type='text'
id='2d_txt_deta il_total_euro' value='' size='10'
onChange=docume nt.forms[0].elements['txt_haschanged '].value
='yes';></td>
</td>
</tr>
<tr>
<td colspan='6'></td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td height='20' valign='top'>
<table width='100%' border='1' cellpadding='0' cellspacing='0'
valign='top' id='formation'>
<tr>
<td width='15%'>&nb sp;<b>GOODS</b></td>
<td width='5%'>&nbs p;<b>UNIT PRICE</b></td>
<td width='10%'>&nb sp;<b>CURRENCY</b></td>
<td width='5%'>&nbs p;<b>QUANTITY</b></td>
<td width='5%'>&nbs p;<b>TOTAL LINE</b></td>
<td width='5%'>&nbs p;<b>UPLIFT (%)</b></td>
<td width='5%'>&nbs p;<b>INCL. UPLIFT</b></td>
<td width='5%'>&nbs p;<b>=EUR</b></td>
<td width='5%'>&nbs p;<b> </b></td>
</tr>
<tr>
<td colspan='10'>
<label id='label_detai l'>NO RECORDS FOUND</label>
</td>
</tr>
<input type='text' id='2d_txt_deta il_ids'
name='2d_txt_de tail_ids' value=''>
<input type='text' id='txt_update_ ids' name='txt_updat e_ids'
value=''>
</table> </td>
</tr>
</table>
</form>
</body>
</html>
thanks for any help, kie
p.s. the funtion setLabel that addresses: <label id='label_detai l'>
fails for a reason unknown to me, any pointers on this too would be
greatly appreciated.
Comment