0

I am trying to get the table td texts by using Jquery and javascript

I have the following

//tables contain bunch of tables
  for(var i = 0; i < tables.length ; i ++){
        var table = tables[i];

        $(table 'td').each(function(){   //I know there is something wrong with my selector.
            $(this).text()
        })

The jquery selector doesn't work in my case. How do I select every td for different table?

Thanks for the help!

4
  • What do you expect table 'td' to do? Commented Jul 17, 2013 at 19:23
  • Your just missing a "+" after the variable table. $(table + 'td') Commented Jul 17, 2013 at 19:27
  • When you say that //tables contain bunch of tables, is tables a JS array or a jQuery selection? Commented Jul 17, 2013 at 19:27
  • @Shawn31313 You can't concatenate an object with a string (at least to do what's probably expected) Commented Jul 17, 2013 at 19:29

1 Answer 1

5

I think you want to use the .find() method:

$(table).find('td').each(function(){

DEMO: http://jsfiddle.net/jfj47/

Of course, an alternative is to use the "context selector":

$("td", table).each(function(){

DEMO: http://jsfiddle.net/jfj47/1/

Also, if tables is just an array (or array-like object) of DOM elements, you don't have to loop and could use:

$(tables).find("td").each(function(){

DEMO: http://jsfiddle.net/jfj47/2/

References:

Sign up to request clarification or add additional context in comments.

Comments

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.