0

I have the following code, which I'm using to create an array of all the tr's in the tbody area of a table.

var $table = $('#mytable');
var $rows = $table.find("tbody tr");

It works fine and contains the data from the table. However, I want to loop over each row and create an array of the value of each cell in that row. I've tried:

for(x=0;x<$rows.length;x++)
{
    var aCells = $rows[x].find("td");
    alert(aCells.length);
}

But the console is showing an error stating that Object # has no method 'find'

Can anyone help me? I just want to loop over each row in the tbody one at a time and create an array of the cell values within that row so I can access a specific cell on each loop.

3 Answers 3

3

This returns DOM not a jQuery object

$rows[x]

You want to use eq()

$rows.eq(x).find("td");

or just use each()

$rows.each(function(){
    var cells = $(this).find("td");
});
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm!! Thank you! Will mark as correct answer as soon at it lets me.
2

You can use .each() to iterate through the rows and you can access its td elements by using the $(this) reference.

Try

$rows.each(function(){
    var aCells = $(this).find("td");
    alert(aCells.length);
});

Comments

2

$rows is not an array. It's a jQuery object. Use $rows.eq(x) instead. See the documentation of .eq()

1 Comment

Thanks lex82, I wasn't aware of the .eq() method so thank you for your response.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.