php - Can I use Jquery to insert a closing </tr> tag and an opening <tr> tag inside a dynamic table? -


i'm trying use code below dynamically add closing tag followed opening creates new row every 3 cells. working, dom inspector shows tr node, problem is, happens tr isn't closing tr tag. i'm new jquery, there i'm doing wrong code?

         <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.1.min.js" type="text/javascript"></script>         <script type="text/javascript">         $(document).ready(function(){         $('td:nth-child(3n)').after('</tr><tr>');         });         </script>          <table id="mytable" width="266" border="1" cellspacing="10" cellpadding="10">         <tbody>           <tr>         <?php         function somelongassfunction(){             return 'hello';             }         function have_products($a){             return $a<=20;             }         $x=0;         while (have_products($x)) {             echo '<td>' . somelongassfunction() . '</td>';             $x++;         //-------------------------------------          /*if (fmod($x,3) == 0) {                         echo '</tr><tr>';                         continue;                         }*/         //--------------------------------------                             if ($x==20){             break;             }            }            ?>         </tr>         </tbody>         </table>     

you can't work on dom selection if html document. dom document hierarchy of nodes, not of tags. tags in html parsed dom document browser. can't add single bit of html , expect parsed dom structure.

instead, you'll need wrapping in jquery. viable approach -- may not efficient.

$('td').each(function(idx) {     if (idx % 3) {         return;     } else {         $(this).nextall(':lt(2)').andself().wrapall('<tr/>');     } }).parent().unwrap(); 

jsfiddle


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -