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');
});
});
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.
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.
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.
grab the second child: $(t). children(). eq(1);
$(selectedTable).find('td').each(function (index, element) {
...
});
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With