Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .parent() does not work

Why the following code fails with:

Error: class_a_jquery_objects[0].parent is not a function

?

HTML:

<div>
    <div class='a b'></div>
    <div class='b c'></div>
    <div class='c a'></div>
</div>    
<div id='log'></div>

JS:

$(function() {
    var class_a_jquery_objects = $(".a");

    $("#log").append(class_a_jquery_objects.length + "<br />");
    $("#log").append(class_a_jquery_objects[0] + "<br />");
    $("#log").append(class_a_jquery_objects[0].parent() + "<br />");
});
like image 807
Misha Moroshko Avatar asked May 28 '10 07:05

Misha Moroshko


1 Answers

class_a_jquery_objects[0] is a DOM element and not a jQuery object. You can't call jQuery methods with it. You need to first wrap it in a jQuery object:

$(class_a_jquery_objects[0]).parent()
like image 128
kgiannakakis Avatar answered Oct 22 '22 00:10

kgiannakakis