{"id":34,"date":"2009-09-26T03:08:12","date_gmt":"2009-09-26T07:08:12","guid":{"rendered":"http:\/\/dan.verweire.com\/?p=34"},"modified":"2009-09-26T03:22:52","modified_gmt":"2009-09-26T07:22:52","slug":"traversing-tables-with-jquery-1","status":"publish","type":"post","link":"http:\/\/dan.verweire.com\/?p=34","title":{"rendered":"traversing tables with jQuery #1"},"content":{"rendered":"<p>While developing a JavaScript GridLayout tool at work today, I ran in to a tough spot while traversing tables with jQuery. What I wanted was to only work with the TR and TD elements within the parent table and not any nested tables. At first, I was using code like this (see HTML later for an example of what I was working with): <\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">$<span class=\"br0\">&#40;<\/span><span class=\"st0\">&#8216;#myTable&#8217;<\/span><span class=\"st0\">&#8216;tr&#8217;<\/span><span class=\"co1\">\/\/do stuff<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"st0\">&#8216;td&#8217;<\/span><span class=\"co1\">\/\/do stuff<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>But the problem with this is that both calls to the &#8216;find&#8217; method would return all TRs or TDs that are decedents of #myTable, no matter how deep. I could have used a selector that would have found the tbody (the first child of the table not shown in the markup but inserted by the browser) and then found all of the TRs that were children of that and so forth. My issue with that is that I&#8217;m not confidant that all browsers insert the tbody element. I&#8217;ve read online that they are supposed to, but for real, who knows? And I don&#8217;t like programming browser-specific stuff. <\/p>\n<p>At any rate, the easiest way I found to accomplish this is to pass the table.rows and rows.cells collections to the jQuery constructor ala:<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">$<span class=\"br0\">&#40;<\/span>$<span class=\"br0\">&#40;<\/span><span class=\"st0\">&#8216;#myTable&#8217;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">attr<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&#8216;rows&#8217;<\/span><span class=\"co1\">\/\/do stuff<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;$<span class=\"br0\">&#40;<\/span>$tr.<span class=\"me1\">attr<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&#8216;cells&#8217;<\/span><span class=\"co1\">\/\/do stuff<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div>\n<\/li>\n<li class=\"li2\">\n<div class=\"de2\"><span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>And boom, no nested tables return. Wonderful. At least I think that&#8217;s what I did. It&#8217;s been a few hours now&#8230;. <\/p>\n<p>The HTML:<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">&lt;table id=&quot;myTable&quot;&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;&lt;tr&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &lt;td&gt;stuff&lt;\/td&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &lt;td&gt;things&lt;\/td&gt;\n<\/div>\n<\/li>\n<li class=\"li2\">\n<div class=\"de2\">&nbsp;&lt;\/tr&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;&lt;tr&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &lt;td colspan=&quot;2&quot;&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &nbsp;&lt;table&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &nbsp; &lt;tr&gt;\n<\/div>\n<\/li>\n<li class=\"li2\">\n<div class=\"de2\">&nbsp; &nbsp; &nbsp;&lt;td&gt;cat&lt;\/td&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &nbsp; &nbsp;&lt;td&gt;dog&lt;\/td&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &nbsp; &lt;\/tr&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &nbsp;&lt;\/table&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; &lt;\/td&gt;\n<\/div>\n<\/li>\n<li class=\"li2\">\n<div class=\"de2\">&nbsp;&lt;\/tr&gt;\n<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&lt;\/table&gt;<\/div>\n<\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>While developing a JavaScript GridLayout tool at work today, I ran in to a tough spot while traversing tables with jQuery. What I wanted was to only work with the TR and TD elements within the parent table and not any nested tables. At first, I was using code like this (see HTML later for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,5],"tags":[9,10,26,11],"_links":{"self":[{"href":"http:\/\/dan.verweire.com\/index.php?rest_route=\/wp\/v2\/posts\/34"}],"collection":[{"href":"http:\/\/dan.verweire.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dan.verweire.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dan.verweire.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/dan.verweire.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34"}],"version-history":[{"count":5,"href":"http:\/\/dan.verweire.com\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":39,"href":"http:\/\/dan.verweire.com\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions\/39"}],"wp:attachment":[{"href":"http:\/\/dan.verweire.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dan.verweire.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dan.verweire.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}