0
var round1 = output.parentNode.parentNode.parentNode.getElementsByTagName('a')[0].innerHTML


Internet explorer 7 gives me the error "Unable to get value of the property 'innerHTML': object is null or undefined" when the code clearly works on ie 9 ff and chrome any suggestions.

Cadell Christo
  • 3,105
  • 3
  • 21
  • 19

2 Answers2

2

Just don't do this, i.e. calling parentNode repeatedly without even knowing what it is. The DOM might look a lot differently than you expect. This is about as smart as not doing any range checks just for convenience.

Use a defensive & flexible approach instead (or, use a framework like jQuery to do the heavy lifting).

function closest(node, tagName) {
  var parent = node.parentNode;
  if (parent) {
    if (parent.tagName && parent.tagName.toUpperCase() === tagName.toUpperCase()) {
      return parent;
    }
    return closest(parent, tagName);
  }
}

var container = closest(node, "table");
var firstLink = container.getElementsByTagName('a')[0];
var round1    = firstLink.innerHTML || "";
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • well if its any constellation the dom its navigating is a rather large menu so i know its structure but obviously my way isn't valid. i dont really understand your code how does it work? which parent node will this get? thankyou – Cadell Christo May 30 '11 at 09:04
  • @Cadell It retrieves the *closest* parent node with a certain node name, hence the function name. Set a debugger breakpoint in your browser's developer tools to see how it works. – Tomalak May 30 '11 at 09:11
  • @Tomalak hi i used your code and it works fine at getting the first node but i still get the error in ie7 when i implement it to select any parentnodes. i even set up several if then statements to check what dom elements were being selected and still i get the error. any suggestions? – Cadell Christo Jun 02 '11 at 09:44
  • @Cadell If you get a "object is null or undefined" error then it is just that: You are calling a property on something that's `null` or `undefined`. This *always* is a result of sloppy programming and it is really easy to avoid: Just check with `typeof` that your object is neither `null` nor `undefined` before you call `innerHTML`. – Tomalak Jun 02 '11 at 09:53
  • 1
    okay i've looked into it some more and worked out my problem - internet explorer seems to parse anchor tags as a parent when it obviously is not a parent in this sense. i've worked around this now thanks so much tomalak – Cadell Christo Jun 02 '11 at 11:06
  • FOR EVERYONE ELSE out there this code is brilliant but it won't work for gathering elements above the first parent because the var container will be referring to the first function only not the functions that are created in the else statement. to fix this define a a var called container before the function then define the container instead of using the return command like "container = parent" not "var container = parent". happy coding! – Cadell Christo Jun 05 '11 at 08:00
  • @Cadell: To go up to a second parent, say from the innermost `` to the next `
    `: `closest(closest(node, 'table'), 'div')`.
    – Tomalak Jun 05 '11 at 08:04
0

It seems that there is no "a"-Tag in output.parentNode.parentNode.parentNode so getElementsByTagName('a')[0] returns null ;)

Van Coding
  • 24,244
  • 24
  • 88
  • 132