Can you make them table insertRow and insertBefore (element) worktogether?

D

DL

Here's the thing, I have this HTML Table, that has multile rows
already, each has an ID, one of them at the bottom section has id of
"SaveData", new TRs are dynamically created upon user interaction, for
instance,
var tbl = document.getElementById ('mytable');
// pick a TR position to insert a new row
var trPos = 6
newTR = tbl.insertRow(trPos);

// then increment the trPos var
// other business rules and functions...

it works all right for a few TR insertions, and then, it begins to
insert a new TR after "SaveData",
that's bad. So, the question is, is there any way to use
tbl.insertRow(trPos) in conjunction with
insertBefore(document.getElementById('SaveData'))
that would ensure every newly inserted TR would be inserted before
"SaveData" TR?

I've looked at insertAdjacentElement method, but that does not seem to
solve the problem.

Sorry I have to ask the question before I got a good js book. Thanks.
 
S

SAM

DL a écrit :
Here's the thing, I have this HTML Table, that has multile rows
already, each has an ID, one of them at the bottom section has id of
"SaveData", new TRs are dynamically created upon user interaction, for
instance,
var tbl = document.getElementById ('mytable');
// pick a TR position to insert a new row
var trPos = 6
newTR = tbl.insertRow(trPos);

// then increment the trPos var
// other business rules and functions...

it works all right for a few TR insertions, and then, it begins to
insert a new TR after "SaveData",

with which browser ?

a "few" ... how much ?
because I can't reproduce it ...
that's bad. So, the question is, is there any way to use
tbl.insertRow(trPos)
that would ensure every newly inserted TR would be inserted before
"SaveData" TR?

each of these 3 functions bellow add a row before the savedatas
(with my Firefox - not tested IE)

<html>
<script type="text/javascript">

function addRow() {
var tbl = document.getElementById ('mytable');
var newTR = tbl.insertRow(tbl.rows.length-1);
var newTD = document.createElement('TD');
newTD.innerHTML = 'test';
newTR.appendChild(newTD);
}

function addRow2() {
var tbl = document.getElementById ('mytable');
tbl = tbl.getElementsByTagName('TBODY')[0];
var newTR = tbl.insertRow(tbl.rows.length);
var newTD = document.createElement('TD');
newTD.innerHTML = 'test';
newTR.appendChild(newTD);
}

function addRow3() {
var tbl = document.getElementById ('mytable');
tbl = tbl.getElementsByTagName('TBODY')[0];
var newTR = tbl.appendChild(document.createElement('TR'));
var newTD = document.createElement('TD');
newTD.innerHTML = 'test';
newTR.appendChild(newTD);
}
</script>

<table id="mytable" border=1>
<thead>
<tr><th>...</th></tr>
</thead>
<tbody>
<tr><td>...</td></tr>
</tbody>
<tfoot>
<tr id="SaveData"><td>datas</td></tr>
</tfoot>
</table>
<p><a href="javascript:addRow();">add 1</a>
<p><a href="javascript:addRow2();">add 2</a>
<p><a href="javascript:addRow3();">add 3</a>
</html>
 
S

SAM

SAM a écrit :
each of these 3 functions bellow add a row before the savedatas
(with my Firefox - not tested IE)

and this forth one works fine too :

<html>
<script type="text/javascript">

function addRow4() {
var tbl = document.getElementById ('SaveData');
var newTR = document.createElement('TR');
var newTD = document.createElement('TD');
newTD.innerHTML = 'test';
newTR.appendChild(newTD);
tbl.parentNode.insertBefore(newTR, tbl);
}
</script>
<table id="mytable" border=1>
<thead>
<tr><th>...</th></tr>
</thead>
<tbody>
<tr><td>...</td></tr>
</tbody>
<tfoot>
<tr id="SaveData"><td>datas</td></tr>
</tfoot>
</table>
<p><a href="javascript:addRow4();">add 4</a>
</html>
 
D

DL

SAM a écrit : ...

Thanks, I'm testing with IE7, your syntax of tbl.rows.length gave me
an idea,
I've now solved this particular problem with a different approach than
yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top