admin管理员组

文章数量:1317894

This is my HTML code

      <div class="tableStyle myWebsiteTable">
      <table cellspacing="0" cellpadding="0" id="site0" class="site active">
        <thead>  
            <tr>  

            </tr>  
        </thead>
        <tbody>
            <tr class="websiteDetails">
                <td colspan="5">
                <div id="websiteDetails0" class="divWebsiteDetails" style="display: block;">
                    <table>
                        <tbody>
                                                        <tr id="190">
                                <td>index</td>
                                <td></td>
                                <td></td>
                            </tr>
                             <tr class="addPage">
                                <td align="center" colspan="5"></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                </td>
            </tr><!--Website Details-->
        </tbody>
      </table>
                <table id="addNewSiteTable">
        <thead> 
          <tr>               
          </tr>
         </thead> 
      </table>
</div>`<br/>

Now table get added dynamically to this structure.I want to write some logic based on the no of table inside the first div.I tried doing this but did not work $('myWebsiteTable').children('table').length)
PLease suggest the correct way to achieve it Thank you

This is my HTML code

      <div class="tableStyle myWebsiteTable">
      <table cellspacing="0" cellpadding="0" id="site0" class="site active">
        <thead>  
            <tr>  

            </tr>  
        </thead>
        <tbody>
            <tr class="websiteDetails">
                <td colspan="5">
                <div id="websiteDetails0" class="divWebsiteDetails" style="display: block;">
                    <table>
                        <tbody>
                                                        <tr id="190">
                                <td>index</td>
                                <td></td>
                                <td></td>
                            </tr>
                             <tr class="addPage">
                                <td align="center" colspan="5"></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                </td>
            </tr><!--Website Details-->
        </tbody>
      </table>
                <table id="addNewSiteTable">
        <thead> 
          <tr>               
          </tr>
         </thead> 
      </table>
</div>`<br/>

Now table get added dynamically to this structure.I want to write some logic based on the no of table inside the first div.I tried doing this but did not work $('myWebsiteTable').children('table').length)
PLease suggest the correct way to achieve it Thank you

Share Improve this question asked Aug 29, 2013 at 15:01 coderunnercoderunner 9351 gold badge18 silver badges33 bronze badges 1
  • THAT WAS ABSOLUTELY STUPID!!!!!!!!! Working LATE can seriously have an impact!!I agree now! – coderunner Commented Aug 29, 2013 at 15:07
Add a ment  | 

4 Answers 4

Reset to default 7

You need to use . before class in Class Selector (“.class”)

Live Demo

$('.myWebsiteTable').children('table').length

Also make sure you have added jQuery and elements are added to DOM.

You may need to use find() as children will give you only first level childs where as find will get all the tables in descendants.

$('.myWebsiteTable').find('table').length 
$('.myWebsiteTable').children('table').length

And if you actually want to count nested tables:

$('.myWebsiteTable table').length

i think it's class

http://api.jquery./class-selector/

$('.myWebsiteTable')

try this, this is more helpfull for you

Demo Here

本文标签: javascriptTaking the count of number of 39table39 tags using jqueryStack Overflow