Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: $this.text is not a function

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?

like image 837
Cruncher Avatar asked Nov 08 '16 11:11

Cruncher


People also ask

How do I fix uncaught TypeError is not a function?

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.

How do I fix uncaught type error?

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.

What is an uncaught TypeError?

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.

Is not a function TypeError?

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.


1 Answers

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
like image 184
Mitya Avatar answered Sep 24 '22 19:09

Mitya