views:

377

answers:

7

Why does the onclick handler below trigger an "elem.parentNode is not a function" error?

<html>
  <head>
   <script type="text/javascript">
     function getParent(elem) {
       var parent = elem.parentNode();
     }
   </script>
  </head>

  <body>
    <div style="border: solid black 2px">
      <span onclick="getParent(this)">hello</span>
    </div>
  </body>
</html>
+3  A: 

Your problem is that parentNode is not a function. Try removing the ().

Paolo Bergantino
+2  A: 

Because parentNode is not a function? Try elem.parentNode without the parenthesis.

Sean Bright
+1  A: 

It's not a function. It's a property. Lose the parentheses.

var parent = elem.parentNode;

Paul Roub
+1  A: 

parentNode is a property not a function. Drop the () and it should work.

Stuart Childs
+1  A: 

parentNode isn't a function, it's a property.

Plan B
+1  A: 

parentNode is a property, not a function.

var parent = element.parentNode;
ifwdev
+1  A: 

it should be

 function getParent(elem) {
   var parent = elem.parentNode;
}
Russ Cam