The task: to create an array with further access to each element through a loop. On the page there are two tables with identifiers. In each table there are cells with the class "can". The array should consist of cells with the class "can" of the first table. Code example:

<table id="tab1"> <tr> <td class="can"></td> <td class="can"></td> <td></td> </tr> </table> <table id="tab2"> <tr> <td class="can"></td> <td></td> <td class="can"></td> </tr> </table> 

Naturally, the tables are much larger, therefore, it is not rational to access each cell via id.

I tried to do this:

 var tb13 = new Array(); tb13 = $('#tab1>td[class=can]'); 

But does not work. Any ideas?

    1 answer 1

    The selector > means not just a child element, i.e. for table this is tr , but not td .

    Therefore, in your case, everything is extremely simple:

     var tb13 = $('#tab1 .can').get(); //или var tb13 = $('#tab1').find('.can').get(); 
    • How it turned out just) Thank you. I'll go reread the documentation. - mccrush
    • 2
      jQuery documentation as well as its sources - extremely useful and interesting reading - Specter
    • 2
      @mccrush, and do not forget to read about the .each () method, since you need to cycle through each element of the table. - Deonis
    • [gray] * offtopic * [/ gray] @Spectre, if I'm not mistaken, the correction: for table child element is not tr , but tbody . - Crasher
    • this depends on the typesetter, in the case of the vehicle, а это tr` - Specter