I wonder whether someone could help me please.
Using some examples I found I've adapted some code, as shown below, that allows a user to add and delete table rows.
Javascript Code
function addRow(addimagetable) {
var table = document.getElementById(addimagetable);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "radio";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
cell2.appendChild(element2);
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "file";
cell3.appendChild(element3);
}
function deleteRow(addimagetable) {
try {
var table = document.getElementById(addimagetable);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var radio = row.cells[0].childNodes[0];
if (null != radio && true == radio.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch (e) {
alert(e);
}
}
HTML Table
<div>
<input type="button" value="Add Row" onclick="addRow('addimagetable')" /> <input type="button" value="Delete Row"
onclick="deleteRow('addimagetable')" />
<table id="addimagetable" width="407" border="1">
<tr>
<td width="20"><input type="radio" name="radio" /></td>
<td width="147"><input type="text" name="title" /></td>
<td width="218"><input type="file" name="image" /></td>
</tr>
</table>
</div>
As you can see from the table code above, the rows are either added or deleted via a button 'click'. What I've been trying to work out is how to get rid of the 'Add' button, instead replacing it with a function whereby if a row has data in cells '2' and '3' a new row below is created.
I've been working on this for a while now and I just can't seem to find a way to get this to work.
I just wondered whether someone could perhaps give me a helping hand and show me where I'm going wrong.
Many thanks
I found your question as I was trying to sort out a similar issue. I used your case (as it was simpler than mine) to troubleshoot the issue... I thought it was only fair to share the outcome with you.
The following code checks every time one of your two monitored inputs in each row loses focus - if both monitored inputs for the row have values (and the row has not caused an additional row to be generated yet) then a new row is added below.
HTML:
<input type="button" value="Add Row" onclick="addRow('addimagetable')" />
<input type="button" value="Delete Row" onclick="deleteRow('addimagetable')" />
<table id="addimagetable" width="407" border="1">
<tr>
<td width="20"><input type="radio" name="radio"/></td>
<td width="147"><input type="text" name="title" onblur="check(1)" /></td>
<td width="218"><input type="file" name="image" onblur="check(1)" /></td>
</tr>
</table>
Script:
<script>
var numrows = 1; // because you are starting with 1 row visible
var rowsarray = ["0", "0"];
function addRow(addimagetable) {
numrows = numrows + 1;
rowsarray[numrows] = "0";
var table = document.getElementById(addimagetable);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "radio";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
cell2.appendChild(element2);
element2 = cell2.getElementsByTagName("input")[0];
element2.setAttribute("onblur","check("+numrows+")");
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "file";
cell3.appendChild(element3);
element3 = cell3.getElementsByTagName("input")[0];
element3.setAttribute("onblur","check("+numrows+")");
}
function deleteRow(addimagetable) {
try {
var table = document.getElementById(addimagetable);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var radio = row.cells[0].childNodes[0];
if(null != radio && true == radio.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
function check(num) {
table = document.getElementById('addimagetable');
row = table.getElementsByTagName("tr")[num-1];
c1 = row.getElementsByTagName("td")[1].getElementsByTagName('input')[0].value;
c2 = row.getElementsByTagName("td")[2].getElementsByTagName('input')[0].value;
if ((c1 !== "") && (c1 !== null ) && (c2 !== "") && (c2 !== null) && (rowsarray[num] !== 1)) { addRow('addimagetable'); rowsarray[num] = 1; }
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.