/    Sign up×
Community /Pin to ProfileBookmark

Providing add/remove table row functionality to a form

I am trying to add functionality to a table that allows users to add and remove table rows, but I am stuck on the JS part of it. The JS I am working with allows users to add/remove table rows that are populated with text input fields, but I want it so where the rows are populated with these elements: 1) multiple select with populated options, 2) file upload box (input type=”file”). I have no idea how to modify the JS below to do this (I am new to JS).

Here is the JS I am working with:

[code]

function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
var element1 = document.createElement(“input”);
element1.type = “checkbox”;
cell1.appendChild(element1);

var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;

var cell3 = row.insertCell(2);
var element2 = document.createElement(“input”);
element2.type = “text”;
cell3.appendChild(element2);
}

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[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount–;
i–;
}
}
}catch(e) {
alert(e);
}
}

[/code]

Here is the HTML:

[code]

<table border=”0″ cellspacing=”0″ cellpadding=”0″ width=”100%” id=”dataTable”>
<tr>
<td valign=”bottom”>&nbsp;</td>
<td valign=”bottom”><label for=”mobileManufacturer”>Manufacturer</label></td>
<td valign=”bottom”><label for=”mobileModel”>Model</label></td>
<td valign=”bottom”><label for=”descriptorFile”>Build Descriptor File (JAD)</label></td>
<td valign=”bottom”><label for=”binaryFile”>Build Binary (JAR, CAD, ZIP)</label></td>
</tr>
<tr>
<td valign=”top”><input type=”checkbox” /></td>
<td valign=”top”>
<select multiple=”multiple” size=”4″ id=”mobileManufacturer”>
<option>Manufacturer 1</option>
<option>Manufacturer 2</option>
<option>Manufacturer 3</option>
<option>Manufacturer 4</option>
<option>Manufacturer 5</option>
<option>Manufacturer 6</option>
</select>
</td>
<td valign=”top”>
<select multiple=”multiple” size=”4″ id=”mobileModel”>
<option>Model 1</option>
<option>Model 2</option>
<option>Model 3</option>
<option>Model 4</option>
<option>Model 5</option>
<option>Model 6</option>
</select>
</td>
<td valign=”top”><input type=”file” id=”descriptorFile” /></td>
<td valign=”top”><input type=”file” id=”binaryFile” /></td>
</tr>
</table>

<br />

<input type=”button” value=”Add Row” onclick=”addRow(‘dataTable’)” />
<input type=”button” value=”Delete Row” onclick=”deleteRow(‘dataTable’)” />

[/code]

Any help is appreciated.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 21.2009 — You should separate your table into a header and body:
[CODE]<table id="dataTable">
<thead>
<tr>
<th valign="bottom">&nbsp;</td>
<th valign="bottom"><label for="mobileManufacturer">Manufacturer</label></th>
<th valign="bottom"><label for="mobileModel">Model</label></th>
<th valign="bottom"><label for="descriptorFile">Build Descriptor File (JAD)</label></th>
<th valign="bottom"><label for="binaryFile">Build Binary (JAR, CAD, ZIP)</label></th>
</tr>
</thead>
<tbody>
<tr></tr>
<tr></tr>
</tbody>
</table>[/CODE]


Then to access the actual data rows:
[CODE]var table = document.getElementById("dataTable")[B].getElementsByTagName("tbody")[0][/B];
var rowCount = table.rows.length;

var row = table.insertRow(rowCount);[/CODE]

This allows for easy separation of the header cells from the body cells.
×

Success!

Help @jjfossey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...