Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr'

Im trying to grab my divs with the class tooltip.

And then do something like this:

var schemes = $(".tooltip");

for (var i in schemes) {
    var scheme = schemes[i];
    console.log(scheme.attr("cost"));
}

But it throws the above error. What am i missing? (Im new to javascript + jquery obviously)

like image 750
Gleiemeister 2000 Avatar asked Nov 23 '12 16:11

Gleiemeister 2000


People also ask

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.

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.

What is TypeError in JS?

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or.


1 Answers

If you use for-loop to iterate jQuery set, you should get the elements with eq() method, but not using square bracket notation (i.e. []). The code like $(".tooltip")[i] will pick up DOM elements, but not jQuery objects.

var schemes = $(".tooltip");
for (var i = 0; i < schemes.length; i++) {
    var scheme = schemes.eq(i);
    console.log(scheme.attr("cost"));
}

However, you may always use each() to iterate jQuery set:

$(".tooltip").each(function() {
    var scheme = $(this);
    console.log(scheme.attr("cost"));
});
like image 71
VisioN Avatar answered Sep 17 '22 21:09

VisioN