Closed Bug 326078 Opened 15 years ago Closed 15 years ago

DOM gives wrong count and navigation index for childNodes

Categories

(Firefox :: General, defect)

x86
Windows XP
defect
Not set
major

Tracking

()

RESOLVED DUPLICATE of bug 26179

People

(Reporter: sunith_nair, Unassigned)

Details

Attachments

(1 file)

POC
1.68 KB, text/html
Details
User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1

I am getting the value of childNode[0] as undefined when I refer to a <tr> and the <tr> has a new line after the tag. The code is placed in the Additional Information.

Reproducible: Always

Steps to Reproduce:
1. Copy the code provided below in the Additional Information and save it as an html file.
2. Open the file and then click on the 1st radiobutton. You will get the length on the child nodes as 5. This is because I have removed the new line characters after <tr> and before </tr>
3. Click on any other radiobutton. You will get the length of the child nodes as 7 and clildNodes[0] and childNodes[6] will be undefined.

Actual Results:  
I got the error as mentioned in Steps to Reproduce


<html>
	<head>
		<title>Test</title>
		<script language="javascript">
	function getPrice(radio) 
	{	
	  try
	  {
		var oRow = radio.parentNode.parentNode; 
		sBrowserType = navigator.appName;
		sOSName = navigator.platform;
		var weight = 0;
		
		alert(sBrowserType + ' and childNodes length = ' + oRow.childNodes.length);
		alert(oRow.childNodes[0].innerHTML + "\n" + oRow.childNodes[1].innerHTML + "\n" + oRow.childNodes[2].innerHTML + "\n" + 

oRow.childNodes[3].innerHTML + "\n" + oRow.childNodes[4].innerHTML + "\n");
		alert(oRow.childNodes[5].innerHTML + "\n" + oRow.childNodes[6].innerHTML + "\n");
	}
	catch(e)
	{
		alert(e.message);
	}
	}
		</script>
	</head>
	<body>
		<table cellspacing="0" rules="all" border="1" id="dgQtyPrice" style="border-collapse:collapse;">
	<tr>
		<td>&nbsp;</td><td>Quantity</td><td>Price</td><td style="display:none;">Weight</td><td>UnitCost</td>
	</tr>
	<tr><td><input type="radio" onclick="getPrice(this);" ID="rdQtySelect" name="rdQtySelect"></td><td>40000</td><td>1990.00</td><td 

style="display:none;">400</td><td>0.050</td></tr>
	<tr>
		<td><input type="radio" onclick="getPrice(this);" ID="rdQtySelect" name="rdQtySelect"></td><td>50000</td><td>2110.00</td><td 

style="display:none;">500</td><td>0.042</td>
	</tr>
	<tr>
		<td><input type="radio" onclick="getPrice(this);" ID="rdQtySelect" name="rdQtySelect"></td><td>75000</td><td>2755.00</td><td 

style="display:none;">750</td><td>0.037</td>
	</tr>
	<tr>
		<td><input type="radio" onclick="getPrice(this);" ID="rdQtySelect" name="rdQtySelect"></td><td>100000</td><td>3310.00</td><td 

style="display:none;">1000</td><td>0.033</td>
	</tr>
		</table>	
	</body>
</html>
Attached file POC
childNodes[6] is not undefined.  childNodes[6] is a text node containing a line break, and childNodes[6].innerHTML is undefined because childNodes[6] isn't an element.

You'll have to skip them, or strip the whitespace yourself, or switch from row.childNodes[...] to row.getElementsByTagName("td")[...].

*** This bug has been marked as a duplicate of 26179 ***
Status: UNCONFIRMED → RESOLVED
Closed: 15 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.