473,320 Members | 1,876 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,320 software developers and data experts.

'parentNode' is null or not an object error

2
I'm new to javascript...I'm getting 'parentNode' is null or not an object error in the following code

Expand|Select|Wrap|Line Numbers
  1.    for (var ci=0;ci<headerLink.parentNode.childNodes.length;ci++) {
  2.         if (headerLink.parentNode.childNodes[ci].tagName && headerLink.parentNode.childNodes[ci].tagName.toLowerCase() == 'span') span = headerLink.parentNode.childNodes[ci];
  3.     }
can anybody tell me what is the error ?
Dec 20 '07 #1
2 8096
gits
5,390 Expert Mod 4TB
hi ...

welcome to TSDN ...

probably headerLink is not a valid node ... so to be sure we need to see more of your code especially the code where headerLink gets assigned a node-reference ...

kind regards
Dec 20 '07 #2
bent27
2
thank you for your reply gits, I hadn't logged in for quite a while, should I include the enitre script file? anyways, I have included it!!

Expand|Select|Wrap|Line Numbers
  1. //BEGIN
  2. addEvent(window, "load", sortables_init);
  3.  
  4. //Common Variables
  5. var SORT_COLUMN_INDEX;
  6. var oldRows = new Array();
  7.  
  8. //=================================================================================
  9. // This function will find all tables with class sortable and make them sortable
  10. //=================================================================================
  11. function sortables_init() {
  12.     // Find all tables with class sortable and make them sortable
  13.     if (!document.getElementsByTagName) return;
  14.     tbls = document.getElementsByTagName("table");
  15.     var sortable =0;
  16.     for (ti=0;ti<tbls.length;ti++) 
  17.     {
  18.         thisTbl = tbls[ti];
  19.         if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id))
  20.         {
  21.             sortable =1;
  22.             var headerTable = eval("document.all."+thisTbl.id+"header"); //To get the header table
  23.             ts_makeSortable(headerTable,thisTbl);  // Pass the header table and detail table
  24.  
  25.         }
  26.     }
  27.     // get sortcolumn and sortarrow
  28.     // append sortcolumn to sa to get the column
  29.     if((document.forms[0].hidSortArrow.value == 'null') || (document.forms[0].hidSortArrow.value == "")){
  30.         document.forms[0].hidSortArrow.value="up";
  31.         document.forms[0].hidSortColumn.value=0;
  32.     } 
  33.  
  34.        var curValue = document.forms[0].hidSortArrow.value;
  35.        if(sortable ==1){
  36.        if (document.all.list_table.rows.length >1) {
  37.            ts_resortTable(eval('document.all.sa'+document.forms[0].hidSortColumn.value), document.all.list_table);
  38.            if(curValue == "up"){
  39.                ts_resortTable(eval('document.all.sa'+document.forms[0].hidSortColumn.value), document.all.list_table);
  40.         }       
  41.     }
  42.        }
  43.  
  44. }
  45. //=================================================================================
  46. // This function will  make the tables sortable
  47. //=================================================================================
  48.  
  49. function ts_makeSortable(headerTable, detailTable) {
  50.     if (detailTable.rows.length<=1) return;
  51.     if (headerTable.rows && headerTable.rows.length > 0)
  52.     {
  53.         var firstRow = headerTable.rows[0];
  54.     }
  55.  
  56.     if (!firstRow) return;
  57.  
  58.     // We have a first row: assume it's the header, and make its contents clickable links
  59.     for (var i=0;i<firstRow.cells.length;i++)
  60.     {
  61.         var cell = firstRow.cells[i];
  62.         var txt = ts_getInnerText(cell);
  63.         //Set the hyperlink for the cell that contains the heading
  64.         cell.innerHTML = '<h1> <a href="#" id=sa'+i+' onclick="ts_resortTable(this,document.all.'+detailTable.id+');return false;">'+txt+'</a><span class="sortarrow"></span></h1>';
  65.     }
  66. }
  67. //=================================================================================
  68. //To get the inner text within a cell
  69. //=================================================================================
  70. function ts_getInnerText(el){
  71.     if (typeof el == "string") return el;
  72.     if (typeof el == "undefined") { return el };
  73.     if (el.innerText) return el.innerText;    //Not needed but it is faster
  74.     var str = "";
  75.  
  76.     var cs = el.childNodes;
  77.     var l = cs.length;
  78.     for (var i = 0; i < l; i++) {
  79.         switch (cs[i].nodeType) {
  80.             case 1: //ELEMENT_NODE
  81.                 str += ts_getInnerText(cs[i]);
  82.                 break;
  83.             case 3:    //TEXT_NODE
  84.                 str += cs[i].nodeValue;
  85.                 break;
  86.         }
  87.     }
  88.     return str;
  89. }
  90. //=================================================================================
  91. //To perform the sorting when clicked on the header links
  92. //=================================================================================
  93. function ts_resortTable(headerLink,detailTable) {
  94.     // get the span
  95.     var span;
  96.     for (var ci=0;ci<headerLink.parentNode.childNodes.length;ci++) {
  97.         if (headerLink.parentNode.childNodes[ci].tagName && headerLink.parentNode.childNodes[ci].tagName.toLowerCase() == 'span') span = headerLink.parentNode.childNodes[ci];
  98.     }
  99.     var spantext = ts_getInnerText(span);
  100.     //var tcolumn = headerLink.parentNode.parentNode;
  101.  
  102.     var column = headerLink.parentNode.parentNode.cellIndex;     //Obtain the cell index of the cell whose hyperlink was clicked.
  103.     var table = detailTable;
  104.  
  105.  
  106.  
  107.     document.forms[0].hidSortColumn.value=column;
  108.  
  109.     // Work out a type for the column
  110.     if (table.rows.length <= 1) return;
  111.     var itm = ts_getInnerText(table.rows[1].cells[column]);
  112.  
  113.     sortfn = ts_sort_caseinsensitive;
  114.     if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
  115.     if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
  116.     if (itm.match(/^\w\w\w\s\d\d[\/-]\d\d[\/-]\d\d\s\d\d\:\d\d\s\w\w$/)) sortfn = ts_sort_date;
  117.     if (itm.match(/^\w\w\w\s\d\d[\/-]\d\d[\/-]\d\d\s\d\d\:\d\d\s\w\w\s$/)) sortfn = ts_sort_date;
  118.     if (itm.match(/^[?$]/)) sortfn = ts_sort_currency;
  119.     if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
  120.  
  121.     SORT_COLUMN_INDEX = column;
  122.  
  123.     if(oldRows.length <= 0){
  124.         for (j=1;j<table.rows.length;j++) 
  125.         { oldRows[j-1] = table.rows[j]; }
  126.     } 
  127.  
  128.     var newRows = new Array(); 
  129.     for(i=0;i<oldRows.length;i++)
  130.         newRows[i] = oldRows[i];
  131.  
  132.      newRows.sort(sortfn);
  133.      if (span.getAttribute("sortdir") == null)
  134.        span.setAttribute('sortdir','up');
  135.  
  136.      if (span.getAttribute("sortdir") == 'up') {
  137.         ARROW = '&nbsp; <img src="img/down_arrow.jpg"  align="center"  border="0">';
  138.         newRows.reverse();
  139.         span.setAttribute('sortdir','down');
  140.  
  141.     } else {
  142.         ARROW = '&nbsp; <img src="img/up_arrow.jpg"  align="center"  border="0">';
  143.         newRows.sort(sortfn);
  144.         span.setAttribute('sortdir','up');
  145.     }
  146.  
  147.         //    ARROW=tcolumn.innerHTML;
  148.  
  149.     document.forms[0].hidSortArrow.value=span.getAttribute("sortdir"); 
  150.  
  151.  
  152.     // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
  153.     // don't do sortbottom rows
  154.     for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
  155.     // do sortbottom rows only
  156.     for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
  157.  
  158.     // Delete any other arrows that may be showing
  159.     var allspans = document.getElementsByTagName("span");
  160.     for (var ci=0;ci<allspans.length;ci++) {
  161.         if (allspans[ci].className == 'sortarrow') {
  162.             if (getParent(allspans[ci],"table") == getParent(headerLink,"table")) { // in the same table as us?
  163.                 allspans[ci].innerHTML = '&nbsp;';
  164.             }
  165.         }
  166.     }
  167.  
  168.     // applying different back ground colors to alternate rows
  169.         for (j=1;j<table.rows.length;j++) 
  170.         { 
  171.             if(j%2 == 0){
  172.                 table.rows[j].bgColor = "#FEFEF5";
  173.             } else {
  174.                 table.rows[j].bgColor = "#BCD2EE";
  175.             }
  176.         }
  177.  
  178.     span.innerHTML = ARROW;
  179. }
  180. //=================================================================================
  181. //To get the parent node
  182. //=================================================================================
  183. function getParent(el, pTagName) {
  184.     if (el == null) return null;
  185.     else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())    // Gecko bug, supposed to be uppercase
  186.         return el;
  187.     else
  188.         return getParent(el.parentNode, pTagName);
  189. }
  190. //=================================================================================
  191. //To do the sorting based on date
  192. //=================================================================================
  193. function ts_sort_date(a,b) {
  194.     // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
  195.     aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  196.     bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  197.  
  198.     if (aa.length >= 21 ) {
  199.         dt1 = aa.substr(10,2)+aa.substr(4,2)+aa.substr(7,2)+aa.substr(19,2)+aa.substr(13,2)+aa.substr(16,2) ;
  200.     }else if (aa.length == 10) {
  201.         dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
  202.     } else {
  203.         yr = aa.substr(6,2);
  204.         if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
  205.         dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
  206.     }
  207.     if (bb.length >= 21) {
  208.         dt2 = bb.substr(10,2)+bb.substr(4,2)+bb.substr(7,2)+bb.substr(19,2)+ bb.substr(13,2)+bb.substr(16,2);
  209.     }else if (bb.length == 10) {
  210.         dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
  211.     } else {
  212.         yr = bb.substr(6,2);
  213.         if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
  214.         dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
  215.     }
  216.  
  217.     if (dt1==dt2) return 0;
  218.     if (dt1<dt2 ) return -1;
  219.  
  220.     return 1;
  221. }
  222. //=================================================================================
  223. //To sort based on currency
  224. //=================================================================================
  225. function ts_sort_currency(a,b) { 
  226.     aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
  227.     bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
  228.     return parseFloat(aa) - parseFloat(bb);
  229. }
  230. //=================================================================================
  231. //To sort numeric values
  232. ////=================================================================================
  233. function ts_sort_numeric(a,b) { 
  234.     aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
  235.     if (isNaN(aa)) aa = 0;
  236.     bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); 
  237.     if (isNaN(bb)) bb = 0;
  238.     return aa-bb;
  239. }
  240. //=================================================================================
  241. //To sort based on caseinsensitive
  242. //=================================================================================
  243. function ts_sort_caseinsensitive(a,b) {
  244.     aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
  245.     bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
  246.     if (aa==bb) return 0;
  247.     if (aa<bb) return -1;
  248.     return 1;
  249. }
  250. //=================================================================================
  251. //To sort on a default column
  252. //=================================================================================
  253. function ts_sort_default(a,b) {
  254.     aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  255.     bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  256.     if (aa==bb) return 0;
  257.     if (aa<bb) return -1;
  258.     return 1;
  259. }
  260. //=================================================================================
  261. //The function is called when the page is loaded
  262. //=================================================================================
  263. function addEvent(elm, evType, fn, useCapture)
  264. // addEvent and removeEvent
  265. // cross-browser event handling for IE5+,  NS6 and Mozilla
  266.  
  267. {
  268.   if (elm.addEventListener){
  269.     elm.addEventListener(evType, fn, useCapture);
  270.     return true;
  271.   } else if (elm.attachEvent){
  272.     var r = elm.attachEvent("on"+evType, fn);
  273.     return r;
  274.   } else {
  275.     alert("Handler could not be removed");
  276.   }
  277. }
  278. //=================================================================================
  279. //The function is called when the page is loaded
  280. //=================================================================================
  281. function doAlert() {
  282.     alert("ContextPath is: " + context_path);
  283. }
  284. //END
Jan 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Michael Hill | last post by:
I am trying to get all the "polyline" tags and then find the parent node above it so I can transform it. var poly = svgDocument.getElementsByTagName("polyline"); for ( var i=0; i < poly.length;...
1
by: JoostV | last post by:
However, I am wondering if I can make the creation of my object depend on the name of the parent node DURING parsing (or even the name of the grandparent node)?? In other words, how can I do...
14
by: anupamjain | last post by:
An interesting thing is happening. My table doesnt have 'TBody', but the elem.parentNode.tagName is returning 'TBody' where elem refers to the 'tr' tag. Shouldnt it be returning 'table' ? ...
1
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div...
2
by: DanielLinn | last post by:
I'm getting an error in IE 6.0.2800 that says "Error: Not implemented." when I try to get a parent. Does it whether or not 'compatibility mode' is on. Here's my code: <!DOCTYPE html PUBLIC...
2
by: amattie | last post by:
Anyone know what the specific difference is between the offsetParent and parentNode properties of a DOM element are? Mozilla's docs on the DOM element...
7
by: webdeveloper | last post by:
Without showing any code, I thought maybe somebody already knows what the error message in the title generally means? But in short, a menu is supposed to display by setting its inline style...
19
by: Michael C | last post by:
If we have something like this object x = null; MessageBox.Show(x.ToString()) we get an error. That kindof makes sense but there is no reason the behaviour couldn't be to return an empty...
10
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following three files. 1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to populate a simple <asp:ListBoxcontrol. 2. The UserDetails.cs file creates a Namespace...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.