473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

IE bug with removeChild

VA
I have the following code (modified) from RobG on this newsgroup...

function moveColumn(table_id,col_id, dir)
{
var table=document.getElementById(table_id);
var idx=GetCellIndex(col_id,table);

var numcols=table.rows[0].cells.length;
var fIdx;
fIdx = (dir=="left")? (idx-1) : (idx+1);
if (fIdx < 0) fIdx=numcols-1;
if (fIdx == numcols) fIdx=0;
var j=table.rows.length;
while (j--) {
var row = table.rows[j];
var x = row.removeChild(row.cells[idx]);
row.insertBefore(x, row.cells[fIdx]);
}
}

The intent, of course, is to swap 2 columns in a table.

Works like a charm in FF 1.x

But in IE when I pass in the second-to-last column in the table with a
direction of "right" to swap it with the last column, the removeChild
removes the bottom-right cell in the table and seems to return null
because I get that yellow error thingy in IE and the script aborts.

Is this a IE bug? Is there a workaround?

Thanks

Nov 8 '05 #1
4 14386
VA wrote:
I have the following code (modified) from RobG on this newsgroup...
Unfortunately you combined the logic for swapping and moving - they are
different things. You have tried to bundle it all into one function,
which I suppose is OK but makes it less re-usable.

function moveColumn(table_id,col_id, dir)
{
var table=document.getElementById(table_id);
var idx=GetCellIndex(col_id,table);

Presumably GetCellIndex() returns the index of the column to move:

function GetCellIndex(id, table)
{
var cells = table.rows[0].cells;
var i = cells.length;
while ( i-- ){
if ( cells[i].id && id == cells[i].id ){
return i;
}
}
return null;
}

The following line should be added to moveColumn just after the call to
GetCellIndex() (in case the id can't be found):

if (null == idx) return;


var numcols=table.rows[0].cells.length;
var fIdx;
fIdx = (dir=="left")? (idx-1) : (idx+1);
if (fIdx < 0) fIdx=numcols-1;
if (fIdx == numcols) fIdx=0;
var j=table.rows.length;
while (j--) {
var row = table.rows[j];
var x = row.removeChild(row.cells[idx]);
row.insertBefore(x, row.cells[fIdx]);


What happens here is that you remove cell 3, then try to insert before
cell 3 which no longer exists in the table (it's in limbo).

The logic for moving left or right is different when you wrap around -
so remove the cell to move (cell[idx]), then look ahead to see if
there's a cell at the destination index (fIdx). If there is, insert the
removed cell immediately before it.

If there's no cell at the destination index, insert the removed cell
before the nextSibling of the one to the left (fIdx-1). There wont be a
nextSibling but that's OK, the cell will simply be appended to the end
of the row.

e.g. if there are 4 cells in the row and cell 0 is removed, there are
only 3 left (0 to 2 inclusive). So insert the removed cell before the
nextSibling of cell 2 (which just it as cell 3).

Replace the line above with:

if (row.cells[fIdx]) {
row.insertBefore(x, row.cells[fIdx])
} else {
row.insertBefore(x, row.cells[fIdx-1].nextSibling);
}

[...]
Here's a working example:
<script type="text/javascript">

function moveColumn(table_id, col_id, dir)
{
var table = document.getElementById(table_id);
var idx = GetCellIndex(col_id, table);
if (null == idx) return;

var numcols=table.rows[0].cells.length;
var fIdx;

fIdx = (dir=="left")? (idx-1) : (idx+1);
if (fIdx < 0) fIdx=numcols-1;
if (fIdx == numcols) fIdx=0;

var j=table.rows.length;
while (j--) {
var row = table.rows[j];
var x = row.removeChild(row.cells[idx]);
if (row.cells[fIdx] ){
row.insertBefore(x, row.cells[fIdx])
} else {
row.insertBefore(x,row.cells[fIdx-1].nextSibling);
}
}
}

function GetCellIndex(id, table)
{
var cells = table.rows[0].cells;
var i = cells.length;
while ( i-- ){
if ( cells[i].id && id == cells[i].id ){
return i;
}
}
return null;
}

</script>
<form action="">
<input type="button" value="Move C right" onClick="
moveColumn('tableA','ENAME','right');
"><br>
<input type="button" value="Move D left" onClick="
moveColumn('tableA','EMPNO','left');
"><br>
</form>
<table id="tableA" cellpadding="5" border="1" cellspacing="5"
summary="0">
<tr><th id="JOB">JOB A</th><th id="MGR">MGR B</th><th
id="ENAME">ENAME C</th><th id="EMPNO">EMPNO D</th></tr>
<tr><td>R0 CA</td><td>R0 CB</td><td>R0 CC</td><td>R0 CD</td></tr>
<tr><td>R1 CA</td><td>R1 CB</td><td>R1 CC</td><td>R1 CD</td></tr>
<tr><td>R2 CA</td><td>R2 CB</td><td>R2 CC</td><td>R2 CD</td></tr>
<tr><td>R3 CA</td><td>R3 CB</td><td>R3 CC</td><td>R3 CD</td></tr>
</table>






--
Rob
Nov 8 '05 #2
VA
Rob, thanks a lot. Appreciate your guidance.

Thanks

Nov 8 '05 #3
VA
Still curious...how come my code was working properly in Firefox? IE is
generally more "tolerant" of things like these!

Nov 8 '05 #4
VA wrote:
Still curious...how come my code was working properly in Firefox? IE is
generally more "tolerant" of things like these!


IE probably does more error correction of source HTML than any other
browser - you can generally toss it any old tag soup and it will make
something of it. Whether that's good or bad has been discussed at great
length in other forums.

But when it comes to DOM, IE is pretty ordinary. Not only does it
occasionally strictly enforce the standard in unexpected ways[1], it is
also lacking in conformance. For example, where you have:

someElement.insertBefore(newChild, refChild)

and refChild doesn't exist, newChild should be appended as the last
child of someElement:

"insertBefore

"Inserts the node newChild before the existing child node refChild.
If refChild is null, insert newChild at the end of the list of
children.

"If newChild is a DocumentFragment object, all of its children are
inserted, in the same order, before refChild. If the newChild is
already in the tree, it is first removed."

<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-952280727>
IE simply doesn't follow the spec.
1. for example, only allowing table rows to be appended to a tbody
element when using appendChild, or only firing the onchange event
for a checkbox after it loses focus. Both can be said to be strictly
consistent with the specification but are counter intuitive.
--
Rob
Nov 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bulk88 | last post by:
How can I emulate DOM level 1 removeChild in IE 4? I already figured out to emulate getElementById by doing this if((!document.getElementById) && document.all) {document.getElementById =...
1
by: JehanNYNJ | last post by:
I need to clear out the HTML contents of a <span> element and then re-add that element as a blank span. Here is the code... var elem = document.getElementById(spanId); var deletedElem =...
8
by: Enzo | last post by:
Hi, I have a <div> created dynamically with 'appendchild', inside it a form. When I press the 'esc' key, inside the <div>, I need to remove them and return to the parent page. Using...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
5
by: Markus Stehle | last post by:
Hi all! I want to remove a node from a nodelist by using RemoveChild. I'm using the following code: XmlNode root = Xml.DocumentElement; XmlNodeList items = root.SelectNodes("Item"); XmlNode...
1
by: Rebecca Tsukalas | last post by:
Hello, I have a problem concerning removeChild. This is the XML structure I use with php: <xml_thing <language1 <site>bla1</site <site>bla2</site <site>bla3</site </language1
6
by: VK | last post by:
I must be missing something very obvious, but my nightly head doesn't work anymore. Press "Insert" button to add <insnodes after each <br>. Now press "Delete" - only even <insare being removed....
5
by: tader | last post by:
Hi, so i got script like that var div_box = document.createElement("div"); div_box.setAttribute("id", "music_box"); div_box.style.top = (Number(cords) - 10) + "px"; div_box.style.left =...
10
by: r_ahimsa_m | last post by:
Hello, On index.html page I have a table with id="property_fields". <table id="property_fields" name="property_fields" border="0"> It contains set of rows with the following IDs: var...
5
by: r_ahimsa_m | last post by:
Hello, I am lerning HTML/CSS/JavaScript. I created HTML page with table "property_fields" containing 24 rows ('tr' elements). I want to remove last 23 rows: var table =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.