Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use jQuery .each() to find a child's children?

Given many TABLE tags on a page, how do I select the TD childred on a selected table.

This is logical, but fails with this error:

Error: uncaught exception: Syntax error, unrecognized expression: [object Object]tr

My code

$(document).ready(function () {
    var selectedTable = $('table').eq('9');

    $(selectedTable).css('border','10px solid green');

    $(selectedTable + 'tr td').each(function(i) {
        $(this).css('border','10px solid blue');
    });

});
like image 410
mmcglynn Avatar asked Oct 31 '11 13:10

mmcglynn


People also ask

How do you get children of children in jQuery?

jQuery children() MethodThe children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.

How would you find a child element with a specific class using jQuery?

Given a jQuery object that represents a set of DOM elements, the . children() method allows us to search through the children of these elements in the DOM tree and construct a new jQuery object from the matching elements.

How do you get the children of the $( this selector?

Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.

How can I get second child in jQuery?

grab the second child: $(t). children(). eq(1);


2 Answers

$(selectedTable).find('td').each(function (index, element) {
    ...
});
like image 106
jbabey Avatar answered Sep 24 '22 08:09

jbabey


selectedTable is a jQuery object, not a string.
You can't use it in a selector.

Instead, you need to use jQuery's traversal API:

selectedTable.find('tr td')
like image 38
SLaks Avatar answered Sep 23 '22 08:09

SLaks