how can i append part of a table based on how many tr it has?

70 views Asked by At

I want to append the "<a></a>" content of a table , if that table is not displaying a pre-determined amount of tr's , possible ?

I set up a fiddle here https://jsfiddle.net/e2n57z97/4/

Any tables found on the page that DO NOT have 10 table rows , will get their "<a></a>" appended to the new table. All tables that have 10 tr's will not be appended.

Example HTML below showing 10 and 5 tr's , so how can i append all "<a></a>" info of any table that isn't showing exactly 10 tr's

<table class="report">  <!--  TABLE WITH 10 TR DO NOT APPEND A TAG INFO -->
    <caption>
      <span>
        <a href="#">DO NOT APPEND ME</a>
      </span>
    </caption>
    <tbody>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
    </tbody>
</table>

<table class="report">  <!--  TABLE WITH LESS THEN 10 TR APPEND A TAG INFO -->
    <caption>
      <span>
        <a href="#">APPEND ME TO NEW TABLE</a>
      </span>
    </caption>
    <tbody>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
    </tbody>
</table>


<table class="homepagemodule report" id="Starting_Lineup" align="center" cellspacing="1">
<caption><span>No Starting Lineup</span></caption>
<tbody>
APPEND INFO HERE
</tbody>
</table>
1

There are 1 answers

2
jhinzmann On BEST ANSWER

This code will loop over all report tables and count the table rows. If there are not exact 10 rows, it appends the all links to the lineup table.

I guess you should add a class to the link and check for that link in the .find("a"). The current code would find all links in the report table and not just one.

jQuery(".report").each(function(index, element){
    if(jQuery(this).find("tr").length !== 10 && jQuery(this).attr("id") !== "Starting_Lineup"){
        var target = jQuery("#Starting_Lineup").find("tbody");
        jQuery(this).find("a").each(function(){
            target.append("<tr><td>" + jQuery(this)[0].outerHTML + "</td></tr>");
        });
    }
})