/    Sign up×
Community /Pin to ProfileBookmark

Delete row (<tr>) in table

I have found it very hard to delete a TR using JavaScript.
I read somewhere that the TR is read-only (why??).

I have TRs with an ID attached to them, so I can edit or delete them as I want to.

<table>
<tbody id=tableId>
<tr id=tr1><td>stuff</td></tr>
<tr id=tr2><td>stuff</td></tr>
<tr id=tr3><td>stuff</td></tr>
</tbody>
</table>

As I see it there are a few ways of deleting a row in a table:

[B]1) The easiest way would be [/B]
document.getElementById(“tr2”).outerHTML = “”;
(Does not work)

[B]2) Loop through the rows in the table[/B]
for (var i = 0; i < document.getElementById(“tableId”).rows.length; i++) {
if (document.getElementById(“tableId”).rows[i].id == “tr2”) {
document.getElementById(“tableId”).deleteRow(i);
}
}
(This works, but is very slow if you want to delete many rows, since for every TR to remove, you have to loop through the entire table each time)

[B]3) Hopefully this would work, but it did not[/B]
document.getElementById(“tableId”).deleteRow(“tr2”);

NOTE: I use IE 5.5.

Any thoughts?
// Michelle

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliAug 28.2003 — I have an example code at [url=http://www.webapplikations.com/pages/html_js/tables/DOMTableManipulation.html] this location[/url]

best part is that its crossbrowser(NS and IE)
×

Success!

Help @michelle 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.20,
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,
)...