hello,
when i create elements and want to assign events to them, i have
realised that if the function assigned to that element has no
parameters, then the parent node values can be attained.
e.g.
aTextBox=docume nt.createElemen t('input');
aTextBox.onchan ge=calculateOne Row2;
will enable the function "calculateOneRo w2" to draw on the
"this.parentNod e;" object.
if however i pass a variable, for example:
aTextBox.onchan ge=function(){c alculateOneRow2 (5);};
then anything attached to "this." doesn't exist. i'm sure this is
fundamental javascript programming, can anyone tell me why this is, or
even how to gain access to the parentNode while at the same time
passing variables to a function?
i'd greatly appreciate any pointers, here's the code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javaScript" >
<!--
var globalClaimLnsR owNum = 1;
function detail_row(s_qu antity,s_incl_u plift)
{
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 ;
var formation = document.getEle mentById('forma tion');
var tablebody2=form ation.getElemen tsByTagName('tb ody')[0];
rowSec1=documen t.createElement ('TR');
cellSec1=docume nt.createElemen t('TD');
currenttext=doc ument.createTex tNode('Intitul' );
rowSec2=documen t.createElement ('TR');
rowSec2.id = 'row_' + globalClaimLnsR owNum;
cellSec5=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_quantity;
aTextBox.id = 'txt_cell_one_' +
globalClaimLnsR owNum;
aTextBox.name = 'txt_cell_one_' +
globalClaimLnsR owNum;
aTextBox.onchan ge=calculateOne Row2;
cellSec5.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec5);
cellSec8=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_incl_uplift;
aTextBox.id = 'txt_cell_two_' +
globalClaimLnsR owNum;
aTextBox.name = 'txt_cell_two_' +
globalClaimLnsR owNum;
aTextBox.onchan ge=calculateOne Row2;
cellSec8.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec8);
cellSec10=docum ent.createEleme nt('TD');
ButtonSupprimer =document.creat eElement('input ');
ButtonSupprimer .type = 'button';
ButtonSupprimer .id = 'cmd_del' +
globalClaimLnsR owNum;
ButtonSupprimer .name = 'cmd_del' +
globalClaimLnsR owNum;
ButtonSupprimer .value = 'remove';
ButtonSupprimer .onclick=remove Row;
cellSec10.appen dChild(ButtonSu pprimer);
rowSec2.appendC hild(cellSec10) ;
tablebody2.appe ndChild(rowSec2 );
formation.appen dChild(tablebod y2);
globalClaimLnsR owNum = globalClaimLnsR owNum + 1;
}
function invoice_row(s_i nvoice_no,s_inv oice_amount)
{
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 ;
var formation = document.getEle mentById('forma tionInvoice');
var tablebody2=form ation.getElemen tsByTagName('tb ody')[0];
rowSec1=documen t.createElement ('TR');
cellSec1=docume nt.createElemen t('TD');
currenttext=doc ument.createTex tNode('Intitul' );
rowSec2=documen t.createElement ('TR');
rowSec2.id = 'row_' + globalClaimLnsR owNum;
cellSec1=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_invoice_no;
aTextBox.name = 'txt_cell_three _' +
globalClaimLnsR owNum;
aTextBox.id = 'txt_cell_three _' + globalClaimLnsR owNum;
aTextBox.onchan ge=function(){c alculateOneRow2 (5);};
cellSec1.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec1);
cellSec5=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_invoice_amoun t;
aTextBox.id = 'txt_cell_four_ ' +
globalClaimLnsR owNum;
aTextBox.name = 'txt_cell_four_ ' +
globalClaimLnsR owNum;
aTextBox.onchan ge=function(){c alculateOneRow2 (5);};
cellSec5.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec5);
cellSec10=docum ent.createEleme nt('TD');
ButtonSupprimer =document.creat eElement('input ');
ButtonSupprimer .id = 'five_invoice_d el' +
globalClaimLnsR owNum;
ButtonSupprimer .name = 'five_invoice_d el' +
globalClaimLnsR owNum;
ButtonSupprimer .type = 'button';
ButtonSupprimer .value = 'del';
ButtonSupprimer .onclick=remove Row;
cellSec10.appen dChild(ButtonSu pprimer);
rowSec2.appendC hild(cellSec10) ;
tablebody2.appe ndChild(rowSec2 );
formation.appen dChild(tablebod y2);
globalClaimLnsR owNum = globalClaimLnsR owNum + 1;
}
function calculateOneRow 1(){ // for element within row id "row_xxxx"
var n_Tab = "either";
for( var node = this.parentNode ; node; node=node.paren tNode){
if(node.tagName =="TR"){
alert('Table:' + n_Tab + ', row:' + node.id.substri ng(4))
}
}
}
function calculateOneRow 2(n_Tab){ // for element within row id
"row_xxxx"
for( var node = this.parentNode ; node; node=node.paren tNode){
if(node.tagName =="TR"){
alert('Table:' + n_Tab + ', row:' + node.id.substri ng(4))
}
}
}
function removeRow() // method of form element within row
{
for( var node = this.parentNode ; node; node=node.paren tNode)
if(node.tagName =="TR"){
node.parentNode .removeChild(no de);
break;
}
}
function removeAllRowsAl lTables(){} // fucntion to remove all rows
from all detail tables
function buttonAction(nB utton)
{
var bStatus = true;
var oForm = document.forms[0];
switch (nButton)
{
case 1: // table1
break;
case 2: // table2
break;
case 2: // both tables
break;
default:
bStatus = false;
}
return (bStatus);
}
//endhide -->
</script>
</HEAD>
<BODY>
<form method="post" action="" id="form1" name="form1">
<table>
<tr>
<td> <input name='cmd_detai l_new' type='button'
id='cmd_detail_ new' value='add row table1' onclick=detail_ row('','');>
</td>
<td> <input name='cmd_invoi ce' type='button' id='cmd_invoice '
value='add row table2' onclick=invoice _row('','');> </td>
<td> <input name='cmd_all' type='button' id='cmd_all'
value='REMOVE ALL ROWS, ALL TABLES' onclick=removeA llRowsAllTables ();>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td height='20' valign='top'> <table width='100%' border='1'
cellpadding='0' cellspacing='0' valign='top' id='formation'>
<thead>
<tr>
<td colspan='8'><b> TABLE1<b></td>
</tr>
<tr>
<td width='15%'>&nb sp;<b>GOODS</b></td>
<td width='5%'>&nbs p;<b>UNIT PRICE</b></td>
<td width='5%'>&nbs p;<b> </b></td>
</tr>
</thead> <tbody> </tbody>
</table>
</tr>
</table>
<br>
<table>
<tr>
<td height='20' valign='top'> <table width='100%' border='1'
cellpadding='0' cellspacing='0' valign='top' id='formationIn voice'>
<thead>
<tr>
<td colspan='8'><b> TABLE2<b></td>
</tr>
<tr>
<td width='15%'>&nb sp;INVOICE NUMBER</td>
<td width='5%'>&nbs p;INVOICE DATE</td>
<td> </td>
</tr>
</thead>
<tbody> </tbody>
</table></td>
</tr>
</table>
</form>
</body>
</html>
thanks for any help, kie
when i create elements and want to assign events to them, i have
realised that if the function assigned to that element has no
parameters, then the parent node values can be attained.
e.g.
aTextBox=docume nt.createElemen t('input');
aTextBox.onchan ge=calculateOne Row2;
will enable the function "calculateOneRo w2" to draw on the
"this.parentNod e;" object.
if however i pass a variable, for example:
aTextBox.onchan ge=function(){c alculateOneRow2 (5);};
then anything attached to "this." doesn't exist. i'm sure this is
fundamental javascript programming, can anyone tell me why this is, or
even how to gain access to the parentNode while at the same time
passing variables to a function?
i'd greatly appreciate any pointers, here's the code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javaScript" >
<!--
var globalClaimLnsR owNum = 1;
function detail_row(s_qu antity,s_incl_u plift)
{
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 ;
var formation = document.getEle mentById('forma tion');
var tablebody2=form ation.getElemen tsByTagName('tb ody')[0];
rowSec1=documen t.createElement ('TR');
cellSec1=docume nt.createElemen t('TD');
currenttext=doc ument.createTex tNode('Intitul' );
rowSec2=documen t.createElement ('TR');
rowSec2.id = 'row_' + globalClaimLnsR owNum;
cellSec5=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_quantity;
aTextBox.id = 'txt_cell_one_' +
globalClaimLnsR owNum;
aTextBox.name = 'txt_cell_one_' +
globalClaimLnsR owNum;
aTextBox.onchan ge=calculateOne Row2;
cellSec5.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec5);
cellSec8=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_incl_uplift;
aTextBox.id = 'txt_cell_two_' +
globalClaimLnsR owNum;
aTextBox.name = 'txt_cell_two_' +
globalClaimLnsR owNum;
aTextBox.onchan ge=calculateOne Row2;
cellSec8.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec8);
cellSec10=docum ent.createEleme nt('TD');
ButtonSupprimer =document.creat eElement('input ');
ButtonSupprimer .type = 'button';
ButtonSupprimer .id = 'cmd_del' +
globalClaimLnsR owNum;
ButtonSupprimer .name = 'cmd_del' +
globalClaimLnsR owNum;
ButtonSupprimer .value = 'remove';
ButtonSupprimer .onclick=remove Row;
cellSec10.appen dChild(ButtonSu pprimer);
rowSec2.appendC hild(cellSec10) ;
tablebody2.appe ndChild(rowSec2 );
formation.appen dChild(tablebod y2);
globalClaimLnsR owNum = globalClaimLnsR owNum + 1;
}
function invoice_row(s_i nvoice_no,s_inv oice_amount)
{
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 ;
var formation = document.getEle mentById('forma tionInvoice');
var tablebody2=form ation.getElemen tsByTagName('tb ody')[0];
rowSec1=documen t.createElement ('TR');
cellSec1=docume nt.createElemen t('TD');
currenttext=doc ument.createTex tNode('Intitul' );
rowSec2=documen t.createElement ('TR');
rowSec2.id = 'row_' + globalClaimLnsR owNum;
cellSec1=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_invoice_no;
aTextBox.name = 'txt_cell_three _' +
globalClaimLnsR owNum;
aTextBox.id = 'txt_cell_three _' + globalClaimLnsR owNum;
aTextBox.onchan ge=function(){c alculateOneRow2 (5);};
cellSec1.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec1);
cellSec5=docume nt.createElemen t('TD');
aTextBox=docume nt.createElemen t('input');
aTextBox.type = 'text';
aTextBox.value = s_invoice_amoun t;
aTextBox.id = 'txt_cell_four_ ' +
globalClaimLnsR owNum;
aTextBox.name = 'txt_cell_four_ ' +
globalClaimLnsR owNum;
aTextBox.onchan ge=function(){c alculateOneRow2 (5);};
cellSec5.append Child(aTextBox) ;
rowSec2.appendC hild(cellSec5);
cellSec10=docum ent.createEleme nt('TD');
ButtonSupprimer =document.creat eElement('input ');
ButtonSupprimer .id = 'five_invoice_d el' +
globalClaimLnsR owNum;
ButtonSupprimer .name = 'five_invoice_d el' +
globalClaimLnsR owNum;
ButtonSupprimer .type = 'button';
ButtonSupprimer .value = 'del';
ButtonSupprimer .onclick=remove Row;
cellSec10.appen dChild(ButtonSu pprimer);
rowSec2.appendC hild(cellSec10) ;
tablebody2.appe ndChild(rowSec2 );
formation.appen dChild(tablebod y2);
globalClaimLnsR owNum = globalClaimLnsR owNum + 1;
}
function calculateOneRow 1(){ // for element within row id "row_xxxx"
var n_Tab = "either";
for( var node = this.parentNode ; node; node=node.paren tNode){
if(node.tagName =="TR"){
alert('Table:' + n_Tab + ', row:' + node.id.substri ng(4))
}
}
}
function calculateOneRow 2(n_Tab){ // for element within row id
"row_xxxx"
for( var node = this.parentNode ; node; node=node.paren tNode){
if(node.tagName =="TR"){
alert('Table:' + n_Tab + ', row:' + node.id.substri ng(4))
}
}
}
function removeRow() // method of form element within row
{
for( var node = this.parentNode ; node; node=node.paren tNode)
if(node.tagName =="TR"){
node.parentNode .removeChild(no de);
break;
}
}
function removeAllRowsAl lTables(){} // fucntion to remove all rows
from all detail tables
function buttonAction(nB utton)
{
var bStatus = true;
var oForm = document.forms[0];
switch (nButton)
{
case 1: // table1
break;
case 2: // table2
break;
case 2: // both tables
break;
default:
bStatus = false;
}
return (bStatus);
}
//endhide -->
</script>
</HEAD>
<BODY>
<form method="post" action="" id="form1" name="form1">
<table>
<tr>
<td> <input name='cmd_detai l_new' type='button'
id='cmd_detail_ new' value='add row table1' onclick=detail_ row('','');>
</td>
<td> <input name='cmd_invoi ce' type='button' id='cmd_invoice '
value='add row table2' onclick=invoice _row('','');> </td>
<td> <input name='cmd_all' type='button' id='cmd_all'
value='REMOVE ALL ROWS, ALL TABLES' onclick=removeA llRowsAllTables ();>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td height='20' valign='top'> <table width='100%' border='1'
cellpadding='0' cellspacing='0' valign='top' id='formation'>
<thead>
<tr>
<td colspan='8'><b> TABLE1<b></td>
</tr>
<tr>
<td width='15%'>&nb sp;<b>GOODS</b></td>
<td width='5%'>&nbs p;<b>UNIT PRICE</b></td>
<td width='5%'>&nbs p;<b> </b></td>
</tr>
</thead> <tbody> </tbody>
</table>
</tr>
</table>
<br>
<table>
<tr>
<td height='20' valign='top'> <table width='100%' border='1'
cellpadding='0' cellspacing='0' valign='top' id='formationIn voice'>
<thead>
<tr>
<td colspan='8'><b> TABLE2<b></td>
</tr>
<tr>
<td width='15%'>&nb sp;INVOICE NUMBER</td>
<td width='5%'>&nbs p;INVOICE DATE</td>
<td> </td>
</tr>
</thead>
<tbody> </tbody>
</table></td>
</tr>
</table>
</form>
</body>
</html>
thanks for any help, kie
Comment