Validate dynamic textbox length - javascript

I have a dynamic table which contains multiple textboxes. I need textbox B to have a maximum of 6 number input and will prompt an error if the input value is less than and not equal to 6. Please help Im new to javascript
function addRow() {
var table = document.getElementById("bod");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.insertCell(0).innerHTML = '<input type="text" name="A" size="20" maxlength="6" required/>';
row.insertCell(1).innerHTML = '<input type="text" name="B" size="20" required/>';
row.insertCell(2).innerHTML = '<input type="text" name="C" size="20" required/>';
}
<input type="button" id="add" value="Add" onclick="Javascript:addRow()">
<table id="bod">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</table>

Create input manually and addEventListener to it. Something like this.
function addRow() {
var table = document.getElementById("bod");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.insertCell(0).innerHTML = '<input type="text" name="A" size="20" maxlength="6" required/>';
var colB = row.insertCell(1);
var inp = document.createElement('input');
inp.type = 'text';
inp.name = 'B';
inp.size = 20;
inp.required = true;
colB.appendChild(inp);
inp.addEventListener('change', function() {
if (this.value.length !== 6) {
alert('wrong value');
this.focus();
}
});
row.insertCell(2).innerHTML = '<input type="text" name="C" size="20" required/>';
}
<input type="button" id="add" value="Add" onclick="addRow()">
<table id="bod">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</table>

Related

To pass id and name through javascript

The output shown on image
Hi am not much expertise in javascript, In my code I have Add-Items & Delete-Items button which is working fine.First row works fine for total calculation function but rest of rows not working because of that Am not able to assign different names and ids for created row. How I can reuse javascript. Also please let me know how to receive data on submit using POST method or this code and how to find total amount of all rows I inserted at the bottom of table.
Thanks in Advance
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
<script type="text/javascript" src="add_rows.js"></script>
<script>
function func()
{
var w = document.getElementById("qty").value;
var x = document.getElementById("price").value;
var z = document.getElementById("total");
z.value = Number(w)*Number(x);
}
</script>
</head>
<body>
<fieldset class="row2">
<legend>Catering Order Details</legend>
<p>
<input type="button" value="Add Items" onClick="addRow('dataTable')" />
<input type="button" value="Remove Items" onClick="deleteRow('dataTable')" />
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<p>
<td><input type="checkbox" required="required" name="chk[]" /></td>
<td>
<label>Item</label>
<select size="1" name="Item[]" required="required" >
<?php
echo "<option value=''>---Choose Item---</option>";
$q=mysqli_query($dbConnect1,"SELECT DISTINCT ITEM as Item, ITEM_ID FROM `manage_item`");
while($r=mysqli_fetch_assoc($q))
{
$i=$r['ITEM_ID'];
$n=$r['Item'];
echo "<option value='$i'> $n</option>";
}
?>
</select>
</td>
<td>
<label>Quantity</label>
<input type="text" required="required" name="Qty[]" id="qty" value="">
</td>
<td>
<label>Price/Quantity</label>
<input type="text" required="required" name="Price[]" id="price" value="" onChange="func()">
</td>
<td>
<label>Total</label>
<input type="text" required="required" name="Total[]" id="total" value="">
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
</div>
</body>
</html>
Here's my script
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 100){
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Maximum items is 100.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot Remove all the Items.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
Instead to use IDs you may change them to class names like in:
<input type="text" required="required" name="Qty[]" class="qty" value="" oninput="func(this)">
Because you are using inline events you can:
use input event because the DOM input event is fired synchronously when the value of an <input>, <select>, or <textarea> element is changed.
this as parameter to the inline function: it will be the current element
Hence, change your func to:
function func(ele) {
var parentRow = ele.parentNode.parentNode;
var w = parentRow.querySelector('input.qty').value;
var x = parentRow.querySelector('input.price').value;
var z = parentRow.querySelector('input.total');
z.value = Number(w)*Number(x);
}
The this parameter now is the ele, current element. You can now get the parent row and using the classes and querySelector you can find all elements.
The snippet:
function updateGrandTotal() {
var gt = document.querySelector('input.grantotal');
gt.value = 0;
document.querySelectorAll('input.total').forEach(function(ele, idx) {
gt.value = +gt.value + +ele.value;
});
}
function func(ele) {
var parentRow = ele.parentNode.parentNode;
var w = parentRow.querySelector('input.qty').value;
var x = parentRow.querySelector('input.price').value;
var z = parentRow.querySelector('input.total');
z.value = Number(w)*Number(x);
updateGrandTotal();
}
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length -1;
if(rowCount < 100){
var row = table.insertRow(rowCount + 1);
var colCount = table.rows[1].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
}
}else{
alert("Maximum items is 100.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot Remove all the Items.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
updateGrandTotal();
}
<fieldset class="row2">
<legend>Catering Order Details</legend>
<p>
<input type="button" value="Add Items" onClick="addRow('dataTable')" />
<input type="button" value="Remove Items" onClick="deleteRow('dataTable')" />
</p>
<table id="dataTable" class="form" border="1">
<thead>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<label>Grand Total</label>
<input type="text" required="required" name="Total[]" class="grantotal" value=""></td>
</tr>
</thead>
<tbody>
<tr>
<p>
<td><input type="checkbox" required="required" name="chk[]" /></td>
<td>
<label>Item</label>
<select size="1" name="Item[]" required="required" >
<option value=''>---Choose Item---</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td>
<label>Quantity</label>
<input type="text" required="required" name="Qty[]" class="qty" value="" oninput="func(this)">
</td>
<td>
<label>Price/Quantity</label>
<input type="text" required="required" name="Price[]" class="price" value="" oninput="func(this)">
</td>
<td>
<label>Total</label>
<input type="text" required="required" name="Total[]" class="total" value="">
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>

How do i access each element in the row?

I am currently working on a college project on the GPA calculator whereby the default subjects, subjects code and credits are filled in the table by default and are editable. The students only need to fill in their expected marks for each subjects and then just simply click on a button that has been provided to view the grades and the pointers for each subject. The following code works just okay since I have not finished a large part of it yet, but there is certainly a problem while running the code. The problem is that the button for showing the grades and pointers for each subject only works on the first row of the table. Which means that it updates the grade and pointer of a subject only on the first row on the table. I have provided you with both the html file and js file to run the program and make you understand more on what I am trying to say.
JS Fiddle example: https://jsfiddle.net/onyhL6mb/
.html file:
<html>
<title> GPA Calculator </title>
<head>
<script src="test_asg3.js">
</script>
</head>
<body>
<form name="gpaCalc">
<table id="myTable" border="1"; width: "100%">
<tr>
<th>Code
<th>Subject
<th>Credit
<th>Expected Mark
<th>Grades
<th>GPA
<th>
</tr>
<tr>
<td><input type="text" name="code" value="SCJ524"></td>
<td><input type="text" name="subject" value="Object-Oriented Programming"></td>
<td><input type="text" name="credit" value="4"></td>
<td><input type="text" id="marks" oninput="getMarks(this.id)"></td>
<td id = "grade"></td>
<td id = "points"></td>
<td><input type="button" value="Show Grades" onclick="displayGrades()" /></td>
</tr>
<tr>
<td><input type="text" name="code" value="SCJ011"></td>
<td><input type="text" name="subject" value="Software Engineering"></td>
<td><input type="text" name="credit" value="3"></td>
<td><input type="text" id="marks1" oninput="getMarks(this.id)"></td>
<td id = "grade"></td>
<td id = "points"></td>
<td><input type="button" value="Show Grades" onclick="displayGrades()" /></td>
</tr>
<tr>
<td><input type="text" name="code" value="SCR234"></td>
<td><input type="text" name="subject" value="Operating System"></td>
<td><input type="text" name="credit" value="3"></td>
<td><input type="text" id="marks2" oninput="getMarks(this.id)"></td>
<td id = "grade"></td>
<td id = "points"></td>
<td><input type="button" value="Show Grades" onclick="displayGrades()" /></td>
</tr>
<tr>
<td><input type="text" name="code" value="SCV122"></td>
<td><input type="text" name="subject" value="Web Programming"></td>
<td><input type="text" name="credit" value="3"></td>
<td><input type="text" id="marks3" oninput="getMarks(this.id)"></td>
<td id = "grade"></td>
<td id = "points"></td>
<td><input type="button" value="Show Grades" onclick="displayGrades()" /></td>
</tr>
<tr>
<td><input type="text" name="code" value="ENG222"></td>
<td><input type="text" name="subject" value="Advanced Academic English Skills"></td>
<td><input type="text" name="credit" value="2"></td>
<td><input type="text" id="marks4" oninput="getMarks(this.id)"></td>
<td id = "grade"></td>
<td id = "points"></td>
<td><input type="button" value="Show Grades" onclick="displayGrades()" /></td>
</tr>
<tr>
<td><input type="text" name="code" value="BIO683"></td>
<td><input type="text" name="subject" value="Structure and Functions of Proteins"></td>
<td><input type="text" name="credit" value="3"></td>
<td><input type="text" id="marks5" oninput="getMarks(this.id)"></td>
<td id = "grade"></td>
<td id = "points"></td>
<td><input type="button" value="Show Grades" onclick="displayGrades()" /></td>
</tr>
</table>
</form>
<br><input type="button" value="Add Subject" onclick="addRow('myTable')" />
<!--<input type="button" value="Calculate GPA" onclick="gpacalc()" /> -->
<!---<br><input type="submit" value="Calculate GPA" onclick="xxxxxx('yyyy')" />--->
</body>
</html>
.js file
function addRow(myTable)
{
var table = document.getElementById("myTable");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
cell2.appendChild(element2);
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
cell3.appendChild(element3);
var cell4 = row.insertCell(3);
var element4 = document.createElement("input");
cell4.appendChild(element4);
var cell5 = row.insertCell(4);
var element5 = document.createElement("");
cell5.appendChild(element5);
var cell6 = row.insertCell(5);
var element6 = document.createElement("");
cell6.appendChild(element6);
var cell7 = row.insertCell(6);
var element7 = document.createElement("");
cell7.appendChild(element7);
}
var x;
function getMarks(id)
{
x = document.getElementById(id).value;
}
function displayGrades()
{
var grade;
var gpaPoint;
if(x >= 90 && x<=100)
{
grade = "A+";
gpaPoint = 4.00;
}
else if(x >=80 && x< 90)
{
grade = "A";
gpaPoint = 4.00;
}
else if(x >=75 && x< 80)
{
grade = "A-";
gpaPoint = 3.67;
}
else if(x >=70 && x< 75)
{
grade = "B+";
gpaPoint = 3.33;
}
else if(x >=65 && x< 70)
{
grade = "B";
gpaPoint = 3.00;
}
else if(x >=60 && x< 65)
{
grade = "B-";
gpaPoint = 2.67;
}
else if(x >=55 && x< 60)
{
grade = "C+";
gpaPoint = 2.33;
}
else if(x >=50 && x< 55)
{
grade = "C";
gpaPoint = 2.00;
}
else if(x >=45 && x< 50)
{
grade = "C-";
gpaPoint = 1.67;
}
else if(x >=40 && x< 45)
{
grade = "D";
gpaPoint = 1.00;
}
else if(x < 40)
{
grade = "F";
gpaPoint = 0.00;
}
document.getElementById("grade").innerHTML = grade;
document.getElementById("points").innerHTML = gpaPoint;
}
I have posted the same question on my other account on here and updated the code using the answers suggested by the other users. However, I am not allowed to post another question on that account since the question that I have posted received "bad reviews" from the other users and hence reached my limit to post another question. Hopefully someone can come up with an idea this time to help with this project.
P/S: it works perfectly fine when i use notepad++ but it doesn't seem to work with the js fiddle example.
You cannot have HTMLElements having the same ID
Your id="grade" and id="point" is wrong.
You should generate it with and unique ID like the row count or the ID of the subject like id="grade_0" or id="grade_SCV122" and pass this variable to your function for exemple.
Edit :
You can do it this way :
Change your button onclick to this
displayGrades(this.parentElement.parentElement)
And the begining of your script like this
function displayGrades( line )
{
var grade;
var gpaPoint;
if( line && line.children && line.children.length > 0 ){
//Pure javascript
grade = line.children[0].children[0].value;
gpaPoint = line.children[3].children[0].value;
//jQuery
grade = $(line).find("input[name=code]").val();
gpaPoint = $(line).find("input[name=marks]").val();//And add the name marks to your input marks
}
UPDATE
//replace the 2 getElementsById lines by that
line.children[4].innerHTML = grade;
line.children[5].innerHTML = gpaPoint;

javascript fields calculation of multiple fields

I am using this code this is html form where i need the javascript onblur calculation of qty * rate = amount
<div>
<p>
<input type="button" value="Add Product" onClick="addRow('dataTable')" />
<input type="button" value="Remove Product" onClick="deleteRow('dataTable')" />
</p>
<table style="width: 100%;" id="dataTable" class="responstable" border="1">
<tbody>
<tr>
<p>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td><input type="text" name="prod" maxlength="100" placeholder="Product *" required></td>
<td>
<input type="number" name="qty[]]" maxlength="10" placeholder="QUANTITY *" required>
</td>
<td>
<input type="number" step="0.01" name="rate[]" maxlength="10" placeholder="RATE *" required>
</td>
<td>
<input type="number" step="0.01" name="amt[]" placeholder="AMOUNT *" required>
</td>
</p>
</tr>
</tbody>
</table>
</div>
And this is Javascript code i am using for add input fields
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 25){ // limit the user from creating fields more than your limits
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Maximum Limit is 25.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) { // limit the user from removing all the fields
alert("Cannot Remove all the Products.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
I need also auto calculation of total amount.
I know it is done by using input field id but here the problem is i don't know how to add different input field ID when i click add product here the same id comes on next input field so what is the best solution for this.
Try this fiddle for dynamic added elements jsfiddle.net/bharatsing/yv9op3ck/2/
HTML:
<div>
<p>
<input type="button" value="Add Product" id="btnAddProduct" />
<input type="button" value="Remove Product" id="btnRemoveProduct" />
<label>Total Amount:</label><label id="lblTotal">0</label>
</p>
<table style="width: 100%;" id="dataTable" class="responstable" border="1">
<tbody>
<tr>
<p>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td><input type="text" name="prod" maxlength="100" placeholder="Product *" required></td>
<td>
<input type="number" name="qty[]" maxlength="10" placeholder="QUANTITY *" required>
</td>
<td>
<input type="number" step="0.01" name="rate[]" maxlength="10" placeholder="RATE *" required>
</td>
<td>
<input type="number" step="0.01" name="amt[]" placeholder="AMOUNT *" required>
</td>
</p>
</tr>
</tbody>
</table>
</div>
Javascript/jQuery:
$("#btnAddProduct").click(function(){
addRow('dataTable');
});
$("#btnRemoveProduct").click(function(){
deleteRow('dataTable');
});
function CalculateAll(){
$('input[name="rate[]"]').each(function(){
CalculateAmount(this);
});
var total=0;
$('input[name="amt[]"]').each(function(){
total+= parseFloat($(this).val());
});
$("#lblTotal").html(total);
}
$(document).on("blur",'input[name="qty[]"]',function(){
CalculateAmount(this);
});
$(document).on("blur",'input[name="rate[]"]',function(){
CalculateAmount(this);
});
var totalAll=0;
function CalculateAmount(ctl){
var tr=$(ctl).parents("tr:eq(0)");
var qty=parseFloat($(tr).find('input[name="qty[]"]').val());
var rate=parseFloat($(tr).find('input[name="rate[]"]').val());
var amount=qty*rate;
$(tr).find('input[name="amt[]"]').val(amount);
if(!isNaN(amount)){
totalAll= totalAll + amount;
$("#lblTotal").html(totalAll);
}
}
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 25){ // limit the user from creating fields more than your limits
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Maximum Limit is 25.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) { // limit the user from removing all the fields
alert("Cannot Remove all the Products.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
CalculateAll();
}
Since in your code you are only using JavaScript. Here is an attempt with JavaScript. You need not to have ID attribute only to calculate the total amount , you can give your amount element a class amount and use it to get sum on all the elements having this class.
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 25){ // limit the user from creating fields more than your limits
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Maximum Limit is 25.");
}
}
function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) { // limit the user from removing all the fields
alert("Cannot Remove all the Products.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
function amount(event)
{
var rate =parseInt(event.target.value, 10);
var qty = parseInt(event.target.parentElement.previousElementSibling.querySelector("input").value, 10);
event.target.parentElement.nextElementSibling.querySelector("input").value = rate * qty;
}
function calculate()
{
var total = 0;
document.querySelectorAll(".amount").forEach(function(elem)
{
total = total + parseInt(elem.value,10);
});
alert(total);
}
<div>
<p>
<input type="button" value="Add Product" onClick="addRow('dataTable')" />
<input type="button" value="Remove Product"onClick="deleteRow('dataTable')" />
</p>
<table style="width: 100%;" id="dataTable" class="responstable" border="1">
<tbody>
<tr>
<p>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td><input type="text" name="prod" maxlength="100" placeholder="Product *" required></td>
<td>
<input type="number" name="qty[]]" maxlength="10" placeholder="QUANTITY *" required>
</td>
<td>
<input type="number" step="0.01" onBlur="amount(event)" name="rate[]" maxlength="10" placeholder="RATE *" required>
</td>
<td>
<input type="number" step="0.01" class ="amount" name="amt[]" placeholder="AMOUNT *" required>
</td>
</p>
</tr>
</tbody>
</table>
<button onClick="calculate()">Total</button>
</div>

how to insert generated html table data into database in php?

How I can insert all data from a generated HTML table into my database using PHP?
I have tried with a foreach loop, but it gives me an error all the time.
I have this code in JavaScript to adding a new row using a button:
var i=1;
function addRow()
{
var tbl = document.getElementById('table1');
var lastRow = tbl.rows.length;
var iteration = lastRow - 1;
var row = tbl.insertRow(lastRow);
var firstCell = row.insertCell(0);
var el = document.createElement('input');
el.type = 'text';
el.name = 'to' + i;
el.id = 'to' + i;
el.size = 40;
firstCell.appendChild(el);
var secondCell = row.insertCell(1);
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'cost' + i;
el2.id = 'cost' + i;
el2.size = 40;
secondCell.appendChild(el2);
frm.h.value=i;
i++;
}
And this is my HTML code:
<table id="table1" width="40%" border="2" cellpadding="0" cellspacing="0">
<tr>
<td><strong>To Address</strong></td>
<td><strong>Delivery Cost</strong></td>
</tr>
<tr>
<td><input name="to" type="text" id="to" size="40"/></td>
<td><input name="cost" type="text" id="cost" size="40"/></td>
</tr>
</table>
<br/><br/>
<input style="float: right;background-color: #57a000;height: 30px;font-weight: bold; font-family: cursive;margin-left: 10px;"
type="submit" value="Save All" name="SaveCost"/>
<input style="float: right;background-color: #57a000;height: 30px;font-weight: bold; font-family: cursive;"
type="button" value="Add New Place" onclick="addRow();"/>
<input name="h" type="hidden" id="h" value="0"/>
Finally I want to write some PHP to insert all the data from the generated HTML table into my database.

How to multiply and check the field of newly inserted row

I want to add new rows having 3 input field and check if multiplication of 2 field equals to third input field. But my problem is if I check the multiplication of first 2 input field, the result will effect second row and third row etc because they all have same id and user can add many rows.
How to check with different id or without effected. my some code are as follows:
function addRow(elem,id) {
var trElement = elem.parentElement.parentElement;
var tr = document.getElementsByTagName('tr');
tr = Array.prototype.slice.call(tr);
var a = tr.indexOf(trElement);
var b = a+1;
var table = document.getElementById('dataTable');
var j, m;
var rowCount = table.rows.length;
var row = table.insertRow(a);
var colCount = table.rows[b].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[b].cells[i].innerHTML;
}
}
<table width="100%" id="dataTable">
<tr>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
</tr>
<tr>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
</tr>
</table>
<input type="button" id="agri" value="Add Row" onclick="addRow(this,id)" />
Thanks in Advance
I hope it Help you ;
cheak result as :
function addRow(elem,id) {
var trElement = elem.parentElement.parentElement;
var tr = document.getElementsByTagName('tr');
tr = Array.prototype.slice.call(tr);
var a = tr.indexOf(trElement);
var b = a+1;
var table = document.getElementById('dataTable');
var j, m;
var rowCount = table.rows.length;
var row = table.insertRow(a);
var colCount = table.rows[b].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[b].cells[i].innerHTML;
}
}
function cheakMulti() {
r = document.getElementsByTagName('tr');
for (i = 0; i < r.length; i++)
{
if(r[i].children[2].children[0].value==r[i].children[1].children[0].value*r[i].children[0].children[0].value)
{
r[i].children[2].children[0].style.backgroundColor="green";
}
else
{
r[i].children[2].children[0].style.backgroundColor="red";
}
}
}
<table width="100%" id="dataTable">
<tr>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
</tr>
<tr>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
<td><input name="" type="text" /></td>
</tr>
</table>
<input type="button" id="agri" value="Add Row" onclick="addRow(this,id)" />
<input type="button" id="agri" value="Cheak" onclick="cheakMulti(this,id)" />

Categories

Resources