Michael Z
Michael Z

Reputation: 3993

Get element from JQuery object array

See my code:

var ths = $("#my_table table th");
if (ths.length > 1) {
    var $th = ths[1]; // tried here plain 'var th' - still got error 
    th.find('#map_column_element_id'); // error here!
}

I get array of JQuery objects. Then I try to manage the second element as JQuery object. When I issue

th.find(...)

I get TypeError: th.find is not a function. What am I doing wrong?

Upvotes: 2

Views: 671

Answers (1)

adeneo
adeneo

Reputation: 318192

You're getting the native JS DOM element, which doesn't have a find() method. Use eq(), or rewrap the element with $(ths[1]).

I'd use eq(), like so:

var ths = $("#my_table table th");
if (ths.length > 1) {
    var $th = ths.eq(1);
    $th.find('#map_column_element_id');
}

Also agree with Andrew in the comment, an ID is unique, and there should be no need to find() it, simply doing $('#map_column_element_id') should suffice.

Upvotes: 6

Related Questions