Solved

Javascript  - add, delete row problem

Posted on 2003-10-29
2
3,768 Views
Last Modified: 2012-08-13
The code is below. i want the delete button to appear on the last row only. As an alternative I dont mind the delete button appears on all rows as long as when a row is deleted the fields left in the form are renamed to 1, 2, 3, 4, 5 etc etc

Many thanks in advance

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
var rowNum = 1;
function addRow(){
tabBody = document.getElementById('name').firstChild;
deleterow = document.getElementById('whatever').firstChild;
newRow = tabBody.appendChild(tabBody.firstChild.cloneNode(true));
rowNum = rowNum + 1;
document.form1.count.value = rowNum;
document.form1.textfieldcount.value = rowNum;
for(i=0;i<newRow.cells.length;i++)
newRow.cells[i].innerHTML = newRow.cells[i].innerHTML.replace(/1([^\d])/g, rowNum+'$1');
}

function deleteRow(node) {
if (rowNum > 1) {
var td = node.parentNode;
while (td.tagName.toLowerCase() != "tr")
td = td.parentNode;
td.parentNode.removeChild(td);
rowNum = rowNum - 1;
document.form1.count.value = rowNum;
}
else {
alert ("You cannot delete the last remaining row");
}
}
</script>
</head>
<body>
<form name="form1" method="get" action="">
  <table width="100%">
    <tr>
      <td width="35%"><input name="count" type="text" class="textbox" id="count" value="1"></td>
      <td><img src="../images/field_add.gif" alt="Add Row" width="14" height="14" align="absmiddle" class="imagebutton" onClick="addRow()"/> <span class="psmall">add</span></td>
    </tr>
  </table>
  <table width="100%" id="name">
    <tr>
      <td width="35%"><input name="textfield1" type="text" class="textbox" id="textfield1">
      <input name="textfieldcount" type="text" class="textbox" id="textfieldcount" value="1" size="2"></td>
      <td id="whatever"><img src="../images/field_delete.gif" alt="Delete //Row"width="14" height="14" align="absmiddle" class="imagebutton" //onClick="deleteRow(this)"/><span class="psmall"> delete</span></td>
    </tr>
  </table>
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
0
Comment
Question by:kate99
2 Comments
 
LVL 11

Accepted Solution

by:
Zontar earned 200 total points
ID: 9642625
You mean something like this?

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function renameRows(tBodyEl)
{  
  var rows = tBodyEl.getElementsByTagName("tr");    
  var row, cell, input, j;
 
  for(var i = 0; i < rows.length; i++)
  {
    j = i + 1;
    row = rows[i];
    cell = row.getElementsByTagName("td")[0];
    input = cell.getElementsByTagName("input")[0];
       
    input.name = "count" + j;    
    input.value = j;
  }
  document.getElementById("textfieldcount").value = rows.length;
}

function addRow()
{
  var table = document.getElementsByTagName("table")[0];
  var tBody = table.getElementsByTagName("tbody")[0];
  var rows = tBody.getElementsByTagName("tr");    
 
  var newRow = tBody.appendChild(rows[0].cloneNode(true));
 
  renameRows(tBody);
}

function removeRow()
{
  var table = document.getElementsByTagName("table")[0];
  var tBody = table.getElementsByTagName("tbody")[0];
  var rows = tBody.getElementsByTagName("tr");  
  var row = rows[0];
 
  if(rows.length > 1)
  {
    tBody.removeChild(row);
    renameRows(tBody);
  }
  else
    alert ("You cannot delete the last remaining row");
}
</script>
</head>
<body>
<form name="form1" method="get" action="">
  <table>
    <tr>
      <td><input name="textfield1" type="text" class="textbox" value="1"></td>
    </tr>
  </table>
  <table>
    <tr>
      <td>
      <input name="textfieldcount" type="text" class="textbox" id="textfieldcount" value="1" size="2"></td>
      <td class="psmall">
        <p><img src="../images/field_add.gif" alt="Add Row"
        width="14" height="14" align="absmiddle" class="imagebutton" onClick="addRow();">add</p>
        <p><img src="../images/field_delete.gif" alt="Delete Row"
             width="14" height="14" align="absmiddle" class="imagebutton" onClick="removeRow();">delete</p>
      delete</td>
    </tr>
  </table>
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
0
 

Author Comment

by:kate99
ID: 9642669
Thanks - seems to work really well
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now