The second statement in this code:
var $this = $(this).children('div.submenu1').children('a.subtile')[0],
title = $this.text(),
name = $this.attr('node').val;
produces this error:
Uncaught TypeError: $this.text is not a function
When I hover over $this.text()
in Chrome debugger I can see the value I want to have in title
.
How can I fix this error?
The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.
Simplest solution is to just use let instead of var . That way, every time through the loop gets a new binding of the variable, and thus the click handlers are all associated with the correct value.
According to the Mozilla website for developer documents, “the TypeError object represents an error when a value is not of the expected type.” Uncaught means that the error was not caught in the catch part of the try-catch block.
This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.
This is happening because you're assigning to the variable $this
a native DOM reference, not a jQuery reference. Only the latter has access to the jQuery API.
You're doing this by 'reaching inside' the array-like jQuery stack and extracting the native reference, via [0]
. Lose this and it'll work:
var $this = $(this).children('div.submenu1').children('a.subtile'),
title = $this.text(), //<-- now has access to jQuery API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With