Hello All,
This is my first post on here. I am new to javascript and I have a problem.
how can i
1. Have a button that adds a new row of cells to a table
2. In these cells there are input boxes/check boxes/dropdown list/ radioboxes and textarea.
3. And have the user input assigned to that specific cell ex. "<textarea rows='3' cols='10' name='desciption.\$i'></textarea>" (I think that how you do it) So that i can query it to a Database
I hope that was clear
Any help is greatly appreciated

-jaycastr

## All 6 Replies

Since you mention querying a database, I assume that you have server-side scripting; if so, you should start there (as they all have a way to do what you want).

That aside, here http://msdn.microsoft.com/en-us/library/ms532998(VS.85).aspx is a complete tutorial using only javascript.

Assuming that a table already exists, you don't need much more from there than `.insertRow` and `.insertCell` . You can generate the widgets by assigning to `.innerHTML` , but - depending on what is already in the table - you may be able to save yourself a lot of work by using the `.cloneNode` method.

And of course you will need an `onclick=` action on the button(s).

Thanks fxm. This is what I got now and it works fine

``````<?php
\$id=\$_POST['id'];
\$deployment=\$_POST['id'];

if(!isset(\$id))
{
echo "no id set";
}
echo "ID = \$id<br><br>";

?>
<html>
<title> Add/Remove dynamic rows in HTML table </title>
<script language="javascript">

var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var colCount = table.rows.cells.length;

for(var i=0; i<colCount; i++) {

var newcell	= row.insertCell(i);

newcell.innerHTML = table.rows.cells[i].innerHTML;
switch(newcell.childNodes.type) {
case "text":
newcell.childNodes.value = "";
break;
case "checkbox":
newcell.childNodes.checked = false;
break;
case "select-one":
newcell.childNodes.selectedIndex = 0;
break;
}
}
}

function deleteRow(tableID) {
try {
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.childNodes;
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}

}catch(e) {
}
}
</script>
<body>
<input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<form method="post" action="predeploymentprocess.php">
<table id="dataTable" width="350px" border="1">
<tr>
<th></th>
<th>Implementor</th>
<th>Start Time</th>
<th>Finish Time</th>
<th>Duration</th>
<th>Status</th>
</tr>
<tr>
<td><input type="checkbox" name="chk[]"/></td>
<td><input type="text" name="imp[]"/></td>
<td><input type="text" name="step[]"/></td>
<td><input type="text" name="start[]"/></td>
<td><input type="text" name="finish[]"/></td>
<td><input type="text" name="dur[]"/></td>
<td>
<select name="status[]">
<option value="">Status?</option>
<option value="incomplete">Incomplete</option>
<option value="complete">Complete</option>
</select>
</td>
</tr>
</table>
<input type="hidden" name="id" value="<?php echo \$id?>"><br />
<input type="submit" value="Enter Data">
</form>
</body>
</html>``````

but now i want to place buttons where start and finsh times are located. and when you press the button it is replaced by text which states the date and time.

some thing like this:

<td><input type="button" name="start" value="Start Time" onclick="starttime()"/></td>

but i cant get the starttime() function to replace the button?
any clues?

but i cant get the starttime() function to replace the button?

No button is needed

``````<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<meta name="generator" content=
"HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
<title>
Add/Remove dynamic rows in HTML table
</title>
<script language="javascript" type="text/javascript">
function timeME(that) {
that.value = new Date()
}
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows.cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows.cells[i].innerHTML;
switch (newcell.childNodes.type) {
case "text":
newcell.childNodes.value = "";
break;
case "checkbox":
newcell.childNodes.checked = false;
break;
case "select-one":
newcell.childNodes.selectedIndex = 0;
break;
}
}
}
function deleteRow(tableID) {
try {
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.childNodes;
if (null != chkbox && true == chkbox.checked) {
if (rowCount <= 1) {
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
} catch (e) {
}
}    </script>
<body>
<form>
"Delete Row" onclick="deleteRow('dataTable')">
</form>
<form method="post" action="predeploymentprocess.php">
<table id="dataTable" width="350px" border="1">
<tr>
<th></th>
<th>
Implementor
</th>
<th>
</th>
<th>
</th>
<th>
Start Time
</th>
<th>
Finish Time
</th>
<th>
Duration
</th>
<th>
Status
</th>
</tr>
<tr>
<td>
<input type="checkbox" name="chk[]">
</td>
<td>
<input type="text" name="imp[]">
</td>
<td>
</td>
<td>
<input type="text" name="step[]">
</td>
<td>
<input onclick="timeME(this)" type="text" name="start[]">
</td>
<td>
<input type="text" name="finish[]">
</td>
<td>
<input type="text" name="dur[]">
</td>
<td>
<select name="status[]">
<option value="">
Status?
</option>
<option value="incomplete">
Incomplete
</option>
<option value="complete">
Complete
</option>
</select>
</td>
</tr>
</table><input type="hidden" name="id" value=
<br>
<input type="submit" value="Enter Data">
</form>
</body>
</html>``````

it works fine

My version of your code was formatted in such a way that it breaks non-IE browsers.
For your code to work in all browsers there must be no whitespace around the checkbox.

``<td><input type="checkbox" name="chk[]"></td>``

(exactly as you originally coded it).

Sorry for the confusion.

BTW: using the name of a method as the name of a function [deleteRow] is not a Good Idea.

There appears to be a problem with `function deleteRow()` .

``````if(rowCount <= 1) {
{"ft_lang_label":"__label__en","ft_lang_prob":0.8790904,"math_prob":0.44529575,"size":522,"snap":"2022-40-2023-06","text_gpt3_token_len":137,"char_repetition_ratio":0.1042471,"word_repetition_ratio":0.0,"special_character_ratio":0.25478926,"punctuation_ratio":0.08035714,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96641856,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-05T02:04:56Z\",\"WARC-Record-ID\":\"<urn:uuid:1aa61d52-3753-4010-8c0c-986d3749cef7>\",\"Content-Length\":\"97386\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:41ff581a-93fe-4299-aa77-4fb6217133ec>\",\"WARC-Concurrent-To\":\"<urn:uuid:aad5061a-c56a-420b-81bc-303965202251>\",\"WARC-IP-Address\":\"172.66.42.251\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/web-development/threads/295487/producing-row-on-a-click-of-a-button\",\"WARC-Payload-Digest\":\"sha1:XOJRAYRBRPQR2PN6KI557OLSYUXPUYSL\",\"WARC-Block-Digest\":\"sha1:VHZZW3OLFME6ISX5SOJRHVEJZFAYRGPR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500158.5_warc_CC-MAIN-20230205000727-20230205030727-00402.warc.gz\"}"}