Accessing table cells from a specific row to retrieve their embedded input value











up vote
1
down vote

favorite












I would like the function to return the result of this action on the values ​​from the table below:




a+b*c-(e/f)




This action is worked whithout row with e and f. I found similar problem here but there is no solution for this one special last row. I'm shure how i should get value from e and f inputs. The function shul be workinf for each row - no matter how many of them will be.






function calc(id) {
var row = id.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = row.cells[0].getElementsByTagName('input')[1].value; //not working
var f = row.cells[1].getElementsByTagName('input')[1].value; //not working

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

body {
font-family: "Calibri";
margin: 0;
}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}

.nr {
font-weight: bold;
}

input {
border: none;
background: transparent;
text-align: center;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}

th {
background-color: #b9c1ce;
}

.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr>
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">Result</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<th align="center" width="5%">E</th>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<th align="center" width="5%">F</th>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
</table>





UPDATE
I wrote a function that was supposed to count the values ​​for data from lines with variables e and f. But unfortunately it lacks to be performed for all rows? I'm not shure how set numbers of rows to serve?






function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
// var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
//var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
//var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c;

row.cells[3].getElementsByTagName('input')[0].value = res;
}

function lastEle()
{
var y = document.getElementById('e').value;
var z = document.getElementById('f').value;

var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
var rowy = mytable.rows.length;

for (var i = 1; i < mytable.rows.length; i++) {

var tmp = mytable.rows[i].myinputs[3].value;
var wynik = +y + +z + +tmp;
mytable.rows[i].myinputs[4].value = wynik;
}
}

 body {
font-family: "Calibri"; margin:0;}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;

}
th{
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr class="cipa">
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">D</th>
<th align="center" width="15%">F</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" id="ads" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" type="text" onkeyup="lastEle();" value="3" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" onkeyup="lastEle();" type="text" name="total" min="0" max="99999999"></td>
</tr>

<tr>
<!-- e -->
<th>e</th>
<td><input type="text" onkeyup="lastEle();" id="e" value="" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" id="f" onkeyup="lastEle();" value="" /></td>
</tr>
</table>












share|improve this question
























  • So when in which moment is problem. Sorry if is a stupid question but i'm trying learning js.
    – simple_code
    Nov 22 at 17:08















up vote
1
down vote

favorite












I would like the function to return the result of this action on the values ​​from the table below:




a+b*c-(e/f)




This action is worked whithout row with e and f. I found similar problem here but there is no solution for this one special last row. I'm shure how i should get value from e and f inputs. The function shul be workinf for each row - no matter how many of them will be.






function calc(id) {
var row = id.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = row.cells[0].getElementsByTagName('input')[1].value; //not working
var f = row.cells[1].getElementsByTagName('input')[1].value; //not working

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

body {
font-family: "Calibri";
margin: 0;
}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}

.nr {
font-weight: bold;
}

input {
border: none;
background: transparent;
text-align: center;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}

th {
background-color: #b9c1ce;
}

.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr>
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">Result</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<th align="center" width="5%">E</th>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<th align="center" width="5%">F</th>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
</table>





UPDATE
I wrote a function that was supposed to count the values ​​for data from lines with variables e and f. But unfortunately it lacks to be performed for all rows? I'm not shure how set numbers of rows to serve?






function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
// var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
//var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
//var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c;

row.cells[3].getElementsByTagName('input')[0].value = res;
}

function lastEle()
{
var y = document.getElementById('e').value;
var z = document.getElementById('f').value;

var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
var rowy = mytable.rows.length;

for (var i = 1; i < mytable.rows.length; i++) {

var tmp = mytable.rows[i].myinputs[3].value;
var wynik = +y + +z + +tmp;
mytable.rows[i].myinputs[4].value = wynik;
}
}

 body {
font-family: "Calibri"; margin:0;}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;

}
th{
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr class="cipa">
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">D</th>
<th align="center" width="15%">F</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" id="ads" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" type="text" onkeyup="lastEle();" value="3" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" onkeyup="lastEle();" type="text" name="total" min="0" max="99999999"></td>
</tr>

<tr>
<!-- e -->
<th>e</th>
<td><input type="text" onkeyup="lastEle();" id="e" value="" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" id="f" onkeyup="lastEle();" value="" /></td>
</tr>
</table>












share|improve this question
























  • So when in which moment is problem. Sorry if is a stupid question but i'm trying learning js.
    – simple_code
    Nov 22 at 17:08













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like the function to return the result of this action on the values ​​from the table below:




a+b*c-(e/f)




This action is worked whithout row with e and f. I found similar problem here but there is no solution for this one special last row. I'm shure how i should get value from e and f inputs. The function shul be workinf for each row - no matter how many of them will be.






function calc(id) {
var row = id.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = row.cells[0].getElementsByTagName('input')[1].value; //not working
var f = row.cells[1].getElementsByTagName('input')[1].value; //not working

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

body {
font-family: "Calibri";
margin: 0;
}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}

.nr {
font-weight: bold;
}

input {
border: none;
background: transparent;
text-align: center;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}

th {
background-color: #b9c1ce;
}

.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr>
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">Result</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<th align="center" width="5%">E</th>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<th align="center" width="5%">F</th>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
</table>





UPDATE
I wrote a function that was supposed to count the values ​​for data from lines with variables e and f. But unfortunately it lacks to be performed for all rows? I'm not shure how set numbers of rows to serve?






function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
// var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
//var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
//var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c;

row.cells[3].getElementsByTagName('input')[0].value = res;
}

function lastEle()
{
var y = document.getElementById('e').value;
var z = document.getElementById('f').value;

var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
var rowy = mytable.rows.length;

for (var i = 1; i < mytable.rows.length; i++) {

var tmp = mytable.rows[i].myinputs[3].value;
var wynik = +y + +z + +tmp;
mytable.rows[i].myinputs[4].value = wynik;
}
}

 body {
font-family: "Calibri"; margin:0;}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;

}
th{
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr class="cipa">
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">D</th>
<th align="center" width="15%">F</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" id="ads" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" type="text" onkeyup="lastEle();" value="3" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" onkeyup="lastEle();" type="text" name="total" min="0" max="99999999"></td>
</tr>

<tr>
<!-- e -->
<th>e</th>
<td><input type="text" onkeyup="lastEle();" id="e" value="" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" id="f" onkeyup="lastEle();" value="" /></td>
</tr>
</table>












share|improve this question















I would like the function to return the result of this action on the values ​​from the table below:




a+b*c-(e/f)




This action is worked whithout row with e and f. I found similar problem here but there is no solution for this one special last row. I'm shure how i should get value from e and f inputs. The function shul be workinf for each row - no matter how many of them will be.






function calc(id) {
var row = id.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = row.cells[0].getElementsByTagName('input')[1].value; //not working
var f = row.cells[1].getElementsByTagName('input')[1].value; //not working

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

body {
font-family: "Calibri";
margin: 0;
}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}

.nr {
font-weight: bold;
}

input {
border: none;
background: transparent;
text-align: center;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}

th {
background-color: #b9c1ce;
}

.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr>
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">Result</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<th align="center" width="5%">E</th>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<th align="center" width="5%">F</th>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
</table>





UPDATE
I wrote a function that was supposed to count the values ​​for data from lines with variables e and f. But unfortunately it lacks to be performed for all rows? I'm not shure how set numbers of rows to serve?






function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
// var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
//var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
//var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c;

row.cells[3].getElementsByTagName('input')[0].value = res;
}

function lastEle()
{
var y = document.getElementById('e').value;
var z = document.getElementById('f').value;

var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
var rowy = mytable.rows.length;

for (var i = 1; i < mytable.rows.length; i++) {

var tmp = mytable.rows[i].myinputs[3].value;
var wynik = +y + +z + +tmp;
mytable.rows[i].myinputs[4].value = wynik;
}
}

 body {
font-family: "Calibri"; margin:0;}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;

}
th{
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr class="cipa">
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">D</th>
<th align="center" width="15%">F</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" id="ads" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" type="text" onkeyup="lastEle();" value="3" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" onkeyup="lastEle();" type="text" name="total" min="0" max="99999999"></td>
</tr>

<tr>
<!-- e -->
<th>e</th>
<td><input type="text" onkeyup="lastEle();" id="e" value="" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" id="f" onkeyup="lastEle();" value="" /></td>
</tr>
</table>








function calc(id) {
var row = id.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = row.cells[0].getElementsByTagName('input')[1].value; //not working
var f = row.cells[1].getElementsByTagName('input')[1].value; //not working

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

body {
font-family: "Calibri";
margin: 0;
}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}

.nr {
font-weight: bold;
}

input {
border: none;
background: transparent;
text-align: center;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}

th {
background-color: #b9c1ce;
}

.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr>
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">Result</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<th align="center" width="5%">E</th>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<th align="center" width="5%">F</th>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
</table>





function calc(id) {
var row = id.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = row.cells[0].getElementsByTagName('input')[1].value; //not working
var f = row.cells[1].getElementsByTagName('input')[1].value; //not working

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

body {
font-family: "Calibri";
margin: 0;
}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}

.nr {
font-weight: bold;
}

input {
border: none;
background: transparent;
text-align: center;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}

th {
background-color: #b9c1ce;
}

.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr>
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">Result</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<th align="center" width="5%">E</th>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<th align="center" width="5%">F</th>
<td align="center" width="15%"><input align="center" type="text" name="total" min="0" max="99999999"></td>
</tr>
</table>





function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
// var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
//var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
//var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c;

row.cells[3].getElementsByTagName('input')[0].value = res;
}

function lastEle()
{
var y = document.getElementById('e').value;
var z = document.getElementById('f').value;

var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
var rowy = mytable.rows.length;

for (var i = 1; i < mytable.rows.length; i++) {

var tmp = mytable.rows[i].myinputs[3].value;
var wynik = +y + +z + +tmp;
mytable.rows[i].myinputs[4].value = wynik;
}
}

 body {
font-family: "Calibri"; margin:0;}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;

}
th{
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr class="cipa">
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">D</th>
<th align="center" width="15%">F</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" id="ads" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" type="text" onkeyup="lastEle();" value="3" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" onkeyup="lastEle();" type="text" name="total" min="0" max="99999999"></td>
</tr>

<tr>
<!-- e -->
<th>e</th>
<td><input type="text" onkeyup="lastEle();" id="e" value="" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" id="f" onkeyup="lastEle();" value="" /></td>
</tr>
</table>





function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
// var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
//var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
//var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c;

row.cells[3].getElementsByTagName('input')[0].value = res;
}

function lastEle()
{
var y = document.getElementById('e').value;
var z = document.getElementById('f').value;

var mytable = document.getElementById('myTable');
var myinputs = mytable.getElementsByTagName('input');
var rowy = mytable.rows.length;

for (var i = 1; i < mytable.rows.length; i++) {

var tmp = mytable.rows[i].myinputs[3].value;
var wynik = +y + +z + +tmp;
mytable.rows[i].myinputs[4].value = wynik;
}
}

 body {
font-family: "Calibri"; margin:0;}

#inTotal {
font-weight: bold;
border-bottom: 2px solid black;
}
.nr {
font-weight: bold;
}
input {
border: none;
background: transparent;
text-align: center;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;

}
th{
background-color: #b9c1ce;
}
.Left {
text-align: left;
margin-left: 5px;
}

<table id="myTable">
<tr class="cipa">
<th align="center" width="5%">A</th>
<th align="center" width="10%">B</th>
<th align="center" width="5%">C</th>
<th align="center" width="15%">D</th>
<th align="center" width="15%">F</th>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" id="ads" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" type="text" onkeyup="lastEle();" value="3" name="total" min="0" max="99999999"></td>
</tr>
<tr>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="quantity" min="1" max="99" onchange="calc(this);"></td>
<td align="center" width="10%"><input onkeyup="calc(this);" type="text" name="summ" min="0" max="999999" onchange="calc(this);"></td>
<td align="center" width="5%"><input onkeyup="calc(this);" type="text" name="rate" min="0" max="100" onchange="calc(this);"></td>
<td align="center" width="15%"><input align="center" type="text" onchange="calc(this);lastEle();" name="total" min="0" max="99999999"></td>
<td align="center" width="15%"><input align="center" onkeyup="lastEle();" type="text" name="total" min="0" max="99999999"></td>
</tr>

<tr>
<!-- e -->
<th>e</th>
<td><input type="text" onkeyup="lastEle();" id="e" value="" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" id="f" onkeyup="lastEle();" value="" /></td>
</tr>
</table>






javascript html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 21:24

























asked Nov 22 at 16:39









simple_code

105




105












  • So when in which moment is problem. Sorry if is a stupid question but i'm trying learning js.
    – simple_code
    Nov 22 at 17:08


















  • So when in which moment is problem. Sorry if is a stupid question but i'm trying learning js.
    – simple_code
    Nov 22 at 17:08
















So when in which moment is problem. Sorry if is a stupid question but i'm trying learning js.
– simple_code
Nov 22 at 17:08




So when in which moment is problem. Sorry if is a stupid question but i'm trying learning js.
– simple_code
Nov 22 at 17:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You did this:



<table>
<tr>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
</tr>
</table>


Assuming row points to <tr>, you can access them this way, where x is the (n+1)th cell and y the (n+1)th input data :



row.cells[x].getElementsByTagName('input')[y];


Here, y could only equal 0, because there is only one input element embedded in the td element.
To access the "e and f" row, simply set an id to their row:



<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>


Then, rewrite your calc() function:



function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}


Keep in mind that you should write another function if e or f are updated: in this case, you'll have to loop the whole rowset to update the values. I let you write this part.



Below a working snippet (without the part of e and f updating).






function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;


var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}

table th,
table td {
border: 1px solid black;
}

table th {
background: #a0a0a0;
}

table td input {
display: block;
width: 100%;
}

<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>








share|improve this answer























  • I think and understand your concept but do not you think it will look ugly? will it be a bad idea to get values from e and f in this way {var e = document.getElementById('e').value;}
    – simple_code
    Nov 22 at 17:37












  • I would like to retrive e from input next to field with E on gray back-ground and the same way f. E and F they are additional variables to which the above rows must refer in the table
    – simple_code
    Nov 22 at 17:54










  • wonderfull I am very grateful. Now I have to sit down and analyze and draw the appropriate conclusions ;).
    – simple_code
    Nov 22 at 18:25










  • You're welcome! I suggest you to start with introduction to DOM and to keep an eye on the DOM reference. MDN Webdocs are very useful for Javascript/DOM programming. (Note: I remove my comments when a SO question is resolved, unless keeping them is relevant.) Happy coding. :)
    – Amessihel
    Nov 22 at 18:29












  • I am asking you to take a look at the functions for e and f values ​​that I could change when filling the table? I update question
    – simple_code
    Nov 22 at 21:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435194%2faccessing-table-cells-from-a-specific-row-to-retrieve-their-embedded-input-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You did this:



<table>
<tr>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
</tr>
</table>


Assuming row points to <tr>, you can access them this way, where x is the (n+1)th cell and y the (n+1)th input data :



row.cells[x].getElementsByTagName('input')[y];


Here, y could only equal 0, because there is only one input element embedded in the td element.
To access the "e and f" row, simply set an id to their row:



<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>


Then, rewrite your calc() function:



function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}


Keep in mind that you should write another function if e or f are updated: in this case, you'll have to loop the whole rowset to update the values. I let you write this part.



Below a working snippet (without the part of e and f updating).






function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;


var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}

table th,
table td {
border: 1px solid black;
}

table th {
background: #a0a0a0;
}

table td input {
display: block;
width: 100%;
}

<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>








share|improve this answer























  • I think and understand your concept but do not you think it will look ugly? will it be a bad idea to get values from e and f in this way {var e = document.getElementById('e').value;}
    – simple_code
    Nov 22 at 17:37












  • I would like to retrive e from input next to field with E on gray back-ground and the same way f. E and F they are additional variables to which the above rows must refer in the table
    – simple_code
    Nov 22 at 17:54










  • wonderfull I am very grateful. Now I have to sit down and analyze and draw the appropriate conclusions ;).
    – simple_code
    Nov 22 at 18:25










  • You're welcome! I suggest you to start with introduction to DOM and to keep an eye on the DOM reference. MDN Webdocs are very useful for Javascript/DOM programming. (Note: I remove my comments when a SO question is resolved, unless keeping them is relevant.) Happy coding. :)
    – Amessihel
    Nov 22 at 18:29












  • I am asking you to take a look at the functions for e and f values ​​that I could change when filling the table? I update question
    – simple_code
    Nov 22 at 21:25















up vote
0
down vote



accepted










You did this:



<table>
<tr>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
</tr>
</table>


Assuming row points to <tr>, you can access them this way, where x is the (n+1)th cell and y the (n+1)th input data :



row.cells[x].getElementsByTagName('input')[y];


Here, y could only equal 0, because there is only one input element embedded in the td element.
To access the "e and f" row, simply set an id to their row:



<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>


Then, rewrite your calc() function:



function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}


Keep in mind that you should write another function if e or f are updated: in this case, you'll have to loop the whole rowset to update the values. I let you write this part.



Below a working snippet (without the part of e and f updating).






function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;


var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}

table th,
table td {
border: 1px solid black;
}

table th {
background: #a0a0a0;
}

table td input {
display: block;
width: 100%;
}

<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>








share|improve this answer























  • I think and understand your concept but do not you think it will look ugly? will it be a bad idea to get values from e and f in this way {var e = document.getElementById('e').value;}
    – simple_code
    Nov 22 at 17:37












  • I would like to retrive e from input next to field with E on gray back-ground and the same way f. E and F they are additional variables to which the above rows must refer in the table
    – simple_code
    Nov 22 at 17:54










  • wonderfull I am very grateful. Now I have to sit down and analyze and draw the appropriate conclusions ;).
    – simple_code
    Nov 22 at 18:25










  • You're welcome! I suggest you to start with introduction to DOM and to keep an eye on the DOM reference. MDN Webdocs are very useful for Javascript/DOM programming. (Note: I remove my comments when a SO question is resolved, unless keeping them is relevant.) Happy coding. :)
    – Amessihel
    Nov 22 at 18:29












  • I am asking you to take a look at the functions for e and f values ​​that I could change when filling the table? I update question
    – simple_code
    Nov 22 at 21:25













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You did this:



<table>
<tr>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
</tr>
</table>


Assuming row points to <tr>, you can access them this way, where x is the (n+1)th cell and y the (n+1)th input data :



row.cells[x].getElementsByTagName('input')[y];


Here, y could only equal 0, because there is only one input element embedded in the td element.
To access the "e and f" row, simply set an id to their row:



<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>


Then, rewrite your calc() function:



function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}


Keep in mind that you should write another function if e or f are updated: in this case, you'll have to loop the whole rowset to update the values. I let you write this part.



Below a working snippet (without the part of e and f updating).






function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;


var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}

table th,
table td {
border: 1px solid black;
}

table th {
background: #a0a0a0;
}

table td input {
display: block;
width: 100%;
}

<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>








share|improve this answer














You did this:



<table>
<tr>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
<td><input type="text" /></td>
</tr>
</table>


Assuming row points to <tr>, you can access them this way, where x is the (n+1)th cell and y the (n+1)th input data :



row.cells[x].getElementsByTagName('input')[y];


Here, y could only equal 0, because there is only one input element embedded in the td element.
To access the "e and f" row, simply set an id to their row:



<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>


Then, rewrite your calc() function:



function calc(element) {
var row = element.parentNode.parentNode;
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;

// Getting the last row
var lastRow = document.getElementById("last-values");

// Then, retrieving e and f
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;

var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}


Keep in mind that you should write another function if e or f are updated: in this case, you'll have to loop the whole rowset to update the values. I let you write this part.



Below a working snippet (without the part of e and f updating).






function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;


var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}

table th,
table td {
border: 1px solid black;
}

table th {
background: #a0a0a0;
}

table td input {
display: block;
width: 100%;
}

<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>








function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;


var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}

table th,
table td {
border: 1px solid black;
}

table th {
background: #a0a0a0;
}

table td input {
display: block;
width: 100%;
}

<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>





function calc(element) {
var row = element.parentNode.parentNode;
var lastRow = document.getElementById("last-values");
var a = row.cells[0].getElementsByTagName('input')[0].value;
var b = row.cells[1].getElementsByTagName('input')[0].value;
var c = row.cells[2].getElementsByTagName('input')[0].value;
var e = lastRow.cells[1].getElementsByTagName('input')[0].value;
var f = lastRow.cells[3].getElementsByTagName('input')[0].value;


var res = +a + +b * c - (e / f);

row.cells[3].getElementsByTagName('input')[0].value = res;
}

table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}

table th,
table td {
border: 1px solid black;
}

table th {
background: #a0a0a0;
}

table td input {
display: block;
width: 100%;
}

<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
<th>res</th>
</tr>
<tr>
<!-- a -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- b -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- c -->
<td><input type="text" onKeyUp="calc(this)" /></td>
<!-- res -->
<td><input type="text" onKeyUp="calc(this)" /></td>
</tr>
<tr id="last-values">
<!-- e -->
<th>e</th>
<td><input type="text" value="5" /></td>
<!-- f -->
<th>f</th>
<td><input type="text" value="6" /></td>
</tr>
</table>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 18:20

























answered Nov 22 at 17:28









Amessihel

1,9241623




1,9241623












  • I think and understand your concept but do not you think it will look ugly? will it be a bad idea to get values from e and f in this way {var e = document.getElementById('e').value;}
    – simple_code
    Nov 22 at 17:37












  • I would like to retrive e from input next to field with E on gray back-ground and the same way f. E and F they are additional variables to which the above rows must refer in the table
    – simple_code
    Nov 22 at 17:54










  • wonderfull I am very grateful. Now I have to sit down and analyze and draw the appropriate conclusions ;).
    – simple_code
    Nov 22 at 18:25










  • You're welcome! I suggest you to start with introduction to DOM and to keep an eye on the DOM reference. MDN Webdocs are very useful for Javascript/DOM programming. (Note: I remove my comments when a SO question is resolved, unless keeping them is relevant.) Happy coding. :)
    – Amessihel
    Nov 22 at 18:29












  • I am asking you to take a look at the functions for e and f values ​​that I could change when filling the table? I update question
    – simple_code
    Nov 22 at 21:25


















  • I think and understand your concept but do not you think it will look ugly? will it be a bad idea to get values from e and f in this way {var e = document.getElementById('e').value;}
    – simple_code
    Nov 22 at 17:37












  • I would like to retrive e from input next to field with E on gray back-ground and the same way f. E and F they are additional variables to which the above rows must refer in the table
    – simple_code
    Nov 22 at 17:54










  • wonderfull I am very grateful. Now I have to sit down and analyze and draw the appropriate conclusions ;).
    – simple_code
    Nov 22 at 18:25










  • You're welcome! I suggest you to start with introduction to DOM and to keep an eye on the DOM reference. MDN Webdocs are very useful for Javascript/DOM programming. (Note: I remove my comments when a SO question is resolved, unless keeping them is relevant.) Happy coding. :)
    – Amessihel
    Nov 22 at 18:29












  • I am asking you to take a look at the functions for e and f values ​​that I could change when filling the table? I update question
    – simple_code
    Nov 22 at 21:25
















I think and understand your concept but do not you think it will look ugly? will it be a bad idea to get values from e and f in this way {var e = document.getElementById('e').value;}
– simple_code
Nov 22 at 17:37






I think and understand your concept but do not you think it will look ugly? will it be a bad idea to get values from e and f in this way {var e = document.getElementById('e').value;}
– simple_code
Nov 22 at 17:37














I would like to retrive e from input next to field with E on gray back-ground and the same way f. E and F they are additional variables to which the above rows must refer in the table
– simple_code
Nov 22 at 17:54




I would like to retrive e from input next to field with E on gray back-ground and the same way f. E and F they are additional variables to which the above rows must refer in the table
– simple_code
Nov 22 at 17:54












wonderfull I am very grateful. Now I have to sit down and analyze and draw the appropriate conclusions ;).
– simple_code
Nov 22 at 18:25




wonderfull I am very grateful. Now I have to sit down and analyze and draw the appropriate conclusions ;).
– simple_code
Nov 22 at 18:25












You're welcome! I suggest you to start with introduction to DOM and to keep an eye on the DOM reference. MDN Webdocs are very useful for Javascript/DOM programming. (Note: I remove my comments when a SO question is resolved, unless keeping them is relevant.) Happy coding. :)
– Amessihel
Nov 22 at 18:29






You're welcome! I suggest you to start with introduction to DOM and to keep an eye on the DOM reference. MDN Webdocs are very useful for Javascript/DOM programming. (Note: I remove my comments when a SO question is resolved, unless keeping them is relevant.) Happy coding. :)
– Amessihel
Nov 22 at 18:29














I am asking you to take a look at the functions for e and f values ​​that I could change when filling the table? I update question
– simple_code
Nov 22 at 21:25




I am asking you to take a look at the functions for e and f values ​​that I could change when filling the table? I update question
– simple_code
Nov 22 at 21:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435194%2faccessing-table-cells-from-a-specific-row-to-retrieve-their-embedded-input-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo