views:

26

answers:

2

I seem to be getting different results of fromDate between Mozilla and IE. Is there a more reliable cross-browser way of doing this? [edit] I am actually getting different column values! [/edit]

<tr id='id3332201010241'  />
 <td>20101024</td>
 <td>20101025</td>
 <td>1415</td>
 <td>1445</td>
 <td>INVDROP</td>  
 <td>H4T1A3</td> 
 <td><a href='#' onclick='selectEditActivity("id3332201010241");' >Click Here</a></td>
</tr>

function selectEditActivity(pass_id){ 
 row = document.getElementById(pass_id);    
 var seq = document.getElementById(pass_id).getAttribute("seq");
 var from_date   = row.childNodes[1].innerHTML; 
 alert(from_date);
 var to_date   = row.childNodes[3].innerHTML;  
}
+1  A: 

You are seeing the result of differences in how different browsers handle white-space. Instead (for more general cases) you can use getElementsByTagName() to for sure get a certain type of child element (<td> in this case), like this:

function selectEditActivity(pass_id){ 
  var row = document.getElementById(pass_id),
      cells = row.getElementsByTagName("td"),
      seq = row.getAttribute("seq"),
      from_date = cells[0].innerHTML,
      to_date = cells[1].innerHTML;  
  alert("From: " + from_date + "\nTo: " + to_date);
}

You can test it out here. As @patrick points out though, it's not needed here, just use the .cells of the <tr>, like this:

function selectEditActivity(pass_id){ 
  var row = document.getElementById(pass_id),
      seq = row.getAttribute("seq"),
      from_date = row.cells[0].innerHTML,
      to_date = row.cells[1].innerHTML;  
  alert("From: " + from_date + "\nTo: " + to_date);
}

You can test it out here.

Nick Craver
You should be able to do it without the `cells = row.getElementsByTagName("td")` since a `<tr>` has its own `cells` property, as in `row.cells[1].innerHTML`.
patrick dw
@patrick - +1 - *completely* overlooked that here, updated to give the much more efficient version
Nick Craver
that works! thanks everyone!
naimer
A: 

You wrongly made <tr> a self-closing tag

<tr id='id3332201010241' />

should be

<tr id='id3332201010241'>

Another thing is that childNodes may not work as you expect since it will contain text nodes. If you want to get <td> elements in a row you should use:

var row       = document.getElementById(pass_id);  
var columns   = row.getElementsByTagName("td"); 
var from_date = columns[1].innerHTML; 
var to_date   = columns[3].innerHTML; 

The childNodes nodeList consists of all child nodes of the element, including (empty) text nodes and comment nodes. @quirksmode

galambalazs