JavaScript - Traversing the HTML DOM using childNodes causes errors in Non IE browsers?

It's because you're using childNodes and whitespaces in the DOM are considered to be text nodes by Firefox et. Al but not IE See this answer for an explanation My suggestions 1. Set up some wrapper functions that ignore any nodeType other than 1 ( ELEMENT_NODE ) to do DOM traversing.

Something like function child(elem, index) { // if index is not supplied, default is 1 // you might be more comfortable making this 0-based // in which case change I initial assignment value to 0 too index = index || 1; // get first child element node of elem elem = (elem. FirstChild && elem.firstChild. NodeType!

= 1)? Next(elem. FirstChild) : elem.

FirstChild; // use the index to move to nth-child element node for(var i=1; I ... ... child(document. GetElementById('myTable'), 2); // will get the tbody window. Onload = function() { document.

GetElementById('getCellContents'). Onclick = getCellContents; } function child(elem, index) { index = index || 1; elem = (elem. FirstChild && elem.firstChild.

NodeType! = 1)? Next(elem.

FirstChild) : elem. FirstChild; for(var i=1; I NodeType! = 1); return elem; } function getCellContents() { var row = parseInt(document.

GetElementById('row'). Value, 10); var column = parseInt(document. GetElementById('column').

Value, 10); var result; var color; var table = document. GetElementById('table'); var cells = table. GetElementsByTagName('td'); for (var i= 0; I BgColor = '#ffffff'; })(); } if (row && column) { var tbody = child(table , 2); var selectedRow = (row Child(tbody, row): null; var selectedCell = (selectedRow && column Child(selectedRow, column): null; if (selectedRow && selectedCell) { selectedCell.

BgColor = '#00ff00'; result = selectedC.

It's because you're using childNodes and whitespaces in the DOM are considered to be text nodes by Firefox et. Al but not IE See this answer for an explanation My suggestions 1. Set up some wrapper functions that ignore any nodeType other than 1 (ELEMENT_NODE) to do DOM traversing.

Something like function child(elem, index) { // if index is not supplied, default is 1 // you might be more comfortable making this 0-based // in which case change I initial assignment value to 0 too index = index || 1; // get first child element node of elem elem = (elem. FirstChild && elem.firstChild. NodeType!

= 1)? Next(elem. FirstChild) : elem.

FirstChild; // use the index to move to nth-child element node for(var i=1; I ... ... 2. Use getElementbyId(), getElementsByTagName() or getElementsByName() instead of relying on position in the DOM 3. Use a JavaScript library that abstracts away browser differences (jQuery comes highly recommended) The Demo Code window.

Onload = function() { document. GetElementById('getCellContents'). Onclick = getCellContents; } function child(elem, index) { index = index || 1; elem = (elem.

FirstChild && elem.firstChild. NodeType! = 1)?

Next(elem. FirstChild) : elem. FirstChild; for(var i=1; I NextSibling; } while (elem && elem.

NodeType! = 1); return elem; } function getCellContents() { var row = parseInt(document. GetElementById('row').

Value, 10); var column = parseInt(document. GetElementById('column'). Value, 10); var result; var color; var table = document.

GetElementById('table'); var cells = table. GetElementsByTagName('td'); for (var i= 0; I Length; i++) { (function() { cellsi. BgColor = '#ffffff'; })(); } if (row && column) { var tbody = child(table , 2); var selectedRow = (row Length)?

Child(tbody, row): null; var selectedCell = (selectedRow && column Length)? Child(selectedRow, column): null; if (selectedRow && selectedCell) { selectedCell. BgColor = '#00ff00'; result = selectedC.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions