Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery throws an error that element.find() is not a function

I have written a small JS to iterate through a set of matched elements and perform some task on each of them.

Here is the code:

var eachProduct = $(".item");

eachProduct.each(function(index, element){

                var eachProductContent = element.find(".product-meta").clone();
});

When I console log element it outputs properly and the exact objects. Why should jquery throw this error?

like image 379
Mostafa Talebi Avatar asked Jun 19 '14 07:06

Mostafa Talebi


4 Answers

because element is a dom element not a jQuery object

var eachProductContent = $(element).find(".product-meta").clone();

Inside the each() handler you will get the dom element reference as the second parameter, not a jQuery object reference. So if you want to access any jQuery methods on the element then you need to get the elements jQuery wrapper object.

like image 151
Arun P Johny Avatar answered Nov 17 '22 00:11

Arun P Johny


You are calling .find() on a plain JS object, But that function belongs to Jquery object

 var eachProductContent = $(element).find(".product-meta").clone();

You can convert it to a jquery object by wrapping it inside $(). And in order to avoid this kind of discrepancies you can simply use $(this) reference instead of using other.

like image 29
Rajaprabhu Aravindasamy Avatar answered Nov 17 '22 01:11

Rajaprabhu Aravindasamy


Use $(this) for current Element

var eachProductContent = $(this).find(".product-meta").clone();
like image 7
Sudharsan S Avatar answered Nov 17 '22 00:11

Sudharsan S


you should change "element" to "this":

var eachProduct = $(".item");

eachProduct.each(function(index, element){

                var eachProductContent = $(this).find(".product-meta").clone();
});
like image 6
Amin Jafari Avatar answered Nov 16 '22 23:11

Amin Jafari