/    Sign up×
Community /Pin to ProfileBookmark

How do I insert a row before a given row….I have written the following code but doesn’t seem to work –

var myTable = document.getElementById(“Matrix”);
var myNewRow = document.createElement(“TR”);
var prevRow = document.getElementById(“r2”);

myTable.insertBefore(myNewRow,prevRow);
myNewRow.innerText = “NewRow”;
myNewRow.id = abc;

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@vmahalinauthorApr 04.2003 — By the way a slight change in the code started to work for me. Instead of writing -

var myTable = document.getElementById("Matrix")

write,

var myTable = document.getElementById("Matrix").firstChild

And the code works.

But in the DHTML way how would you insert a row above another........I mean what is the alternative in DHTML for insertBefore method.
Copy linkTweet thisAlerts:
@khalidali63Apr 04.2003 — what you did ofcourse it works,but, for IE only.

? ,

Then there are all other ways to do this in IE just as Dave posted the links above.

For pure DOM code that works in NS6+ and IE take a look at this link.

http://68.145.35.86/skills/javascripts/InsertNewTR.html

Cheers

Khalid
Copy linkTweet thisAlerts:
@vmahalinauthorApr 04.2003 — Thanks for the prompt reply, but will the code still work if I had to start from an empty table, and create rows dynamically.
Copy linkTweet thisAlerts:
@khalidali63Apr 04.2003 — it shoudl because if the refNode is null then it actually appendsnode to the end,which is what you want to happen when there is no tr's

if not then appendChild is alwasy there for rescue..:-)

Cheers

Khalid
Copy linkTweet thisAlerts:
@vmahalinauthorApr 04.2003 — Thanks Dave for all the help. Do you have links for books specific to Javascript for IE....as one of the prerequisites of our products is usage of only Internet Explorer
×

Success!

Help @vmahalin 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...