1

when a user clicks on a cell in my table, i would like to iterate through my table's rows using the following:

var table = $(this).closest('table');

$(table+" tr").each(function(){
        console.log($(this).text());
    });

However i get the following error:

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

What am i doing wrong?

2 Answers 2

8

table is an object, not a string. Try this:

$('tr', table).each(function() {

That will select tr elements which are descendants of the table.

You could also do this:

table.find('tr').each(function() { 
1

table is a jQuery collection. Its toString method returns [object Object]. When you concatenate a string to the object, it uses its toString to build the resulting string.

"[object Object] tr" is not a valid selector expression. You can select descendants of a jQuery collection using .find or supplying the collection as the second argument to the jQuery method (a context for the selector).

Note that .each is not necessarily required for iteration. It depends on what you need to do.

console.log($(this).closest("table").find("tr").text());

http://jsfiddle.net/3MDgy/1/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.