charlie
charlie

Reputation: 1384

Javascript add link text when adding table rows

i have this JS and HTML Code:

when you click the add button it runs the below function to add another row to the HTML table and adds more form fields with numbers after counting up 1 each time

<script language="javascript" type="text/javascript">
var i=2;
function addRow()
{
          var tbl = document.getElementById('table1');
          var lastRow = tbl.rows.length;
          var iteration = lastRow - 1;
          var row = tbl.insertRow(lastRow);

          var productcodeCell = row.insertCell(0);
          var elproductcode = document.createElement('input');
          elproductcode.type = 'text';
          elproductcode.name = 'productcode' + i;
          elproductcode.id = 'productcode' + i;
          elproductcode.size = 20;
          productcodeCell.appendChild(elproductcode);

          var producttitleCell = row.insertCell(1);
          var elproducttitle = document.createElement('input');
          elproducttitle.type = 'text';
          elproducttitle.name = 'producttitle' + i;
          elproducttitle.id = 'producttitle' + i;
          elproducttitle.size = 30;
          producttitleCell.appendChild(elproducttitle);

          var quantityCell = row.insertCell(2);
          var elQuantity = document.createElement('input');
          elQuantity.type = 'text';
          elQuantity.name = 'quantity' + i;
          elQuantity.id = 'quantity' + i;
          elQuantity.size = 10;
          quantityCell.appendChild(elQuantity);

          i++;
          form1.number.value=i;
          //alert(i);

}
</script>

<form method="post" action="create_quote2.php" name="form1">
<input type="button" value="Add New Line" onclick="addRow();" />
<table width="800" border="0" cellspacing="5" cellpadding="5" id="table1">
  <tr>
    <td><strong>Product Code</strong></td>
    <td><strong>Title</strong></td>
    <td><strong>Add</strong></td>
    <td><strong>Qty</strong></td>
  </tr>
  <tr>
    <td><input type="text" name="productcode1" id="productcode1" size="20" /></td>
    <td><input type="text" name="producttitle1" id="producttitle1" size="30" /></td>
    <td><a href="page.php?c=1">Link here</a></td>
    <td><input type="text" name="quantity1" id="quantity1" size="10" /></td>
  </tr>
  </table>
    <input type="hidden" name="number" id="number" value="1" />
    </form>

how can i add a link in the 3rd column when a row is added also changing the end number in the URL + 1 each time like the other fields

Upvotes: 1

Views: 4884

Answers (1)

Stuart Kershaw
Stuart Kershaw

Reputation: 17671

You could try setting up a variable containing a static link string with i concatenated. Appending the variable where needed should seem familiar given the rest of your script.

var i=2;
function addRow()
{
    var link='/directoryname/filename'+i+'.html';

The new link cell can be built and appended as so:

var linkCell = row.insertCell(2);
var elLink = document.createElement('a');
var href='/directoryname/filename'+i+'.html';
elLink.href = href;
elLink.innerHTML = 'link'+i;
linkCell.appendChild(elLink);

I've added a jsfiddle for you to examine: http://jsfiddle.net/KKKZ8/

Upvotes: 3

Related Questions