3

I'm trying to do a simple loop and check if nodes/parentNode class name matches a string in a array. Code is as follows:

function isInside(list,node) {
    while( node !== undefined ) {
        for( var i = 0; i < list.length; i++ )
            if( node.className.indexOf(list[i]) > -1 )
                return true;
        node = node.parentNode;
    }
    alert(1); // The code does not reach this when false
    return false;
}

Any ideas what is wrong here?

Hauleth
  • 22,873
  • 4
  • 61
  • 112
user1163278
  • 411
  • 6
  • 20
  • Change `node !== undefined` to `node !== null` since `null` will be the result of there being no `.parentNode`. You could also use `!= null`, which will test for both `undefined` and `null`, but it shouldn't be necessary. – the system Feb 25 '13 at 23:12
  • Changing 'node !== undefined' to 'node != null' did not work.. Any other ideas? – user1163278 Feb 25 '13 at 23:18
  • [Works for me](http://jsfiddle.net/qxAt4/) – the system Feb 25 '13 at 23:31

1 Answers1

3

Follow this pattern:

var current = node;
while (current.parentNode){
 // do stuff with node
 current = current.parentNode

}
Mohsen
  • 64,437
  • 34
  • 159
  • 186