Today I was working with jQuery in a JavaScript file, and jQuery kept coughing up a strange error that was puzzling.
elem.nodeName is undefined
Inspecting the backtrace in firebug led me to this line
$( 'td.qty_sel :input' ).live( 'change', ns.viewModel().qtyChange() );
Seasoned JavaScript developers will probably spot the problem immediately, but it took me some time (and a developer friend) to figure it out.
He said, “because your function is not a function reference, it’s a function call”
A few seconds and a corrected script later and it was working.
$( 'td.qty_sel :input' ).live( 'change', ns.viewModel().qtyChange );
Take away:
- When referencing existing functions in a jQuery event handler, don’t call the function (duh) – remove the parens (duh).
🙂