Clearing a table - JQuery
<p>Not quite sure what's going sideways here. </p>

<p>The below function should clear all the rows and then create new rows, but while it does clear on-screen for a moment, it keeps the previous rows and appends to them.</p>

<p>Is this because of the use of appendTo? Why should it matter when it's occurring after the rows have been cleared?</p>

<p>Is there another / better way to clear a table and write an array?</p>

<pre><code>&lt;script type="text/javascript"&gt;
    function buildrow(){
        $( "#oatable tbody tr" ).each( function(){
            this.parentNode.removeChild( this ); 
              });

        $('input:checkbox:checked').each(function() { 
                var idtofind = ($(this).attr('id'));
                //alert(idtofind);
                //alert(idtofind);          
                //Hide Content (this is sloppy but easy to customize..)
                //$(wrapper).hide();
                //('This is the first ' + idtofind);    
                //Get XML Data
                $.ajax({
                type: "GET",
                url: "xml/OA.xml",
                dataType: "xml",
                success: function(xml) {
                    var tablediv = $('div#table');
                    var xmlArr = [];
                    $(xml).find('OAData').each(function(){
                                                if($(this).attr('OAval')===idtofind) {
                            //alert('This is the next ' + idtofind);
                        var xml_OAval           = $(this).attr('OAval');
                        var xml_OAid            = $(this).find('OAid').text();
                        var xml_BAC_Level_1         = $(this).find('BAC_Level_1').text();
                        var xml_Basel_Level_1       = $(this).find('Basel_Level_1').text();
                        var xml_Basel_Level_2       = $(this).find('Basel_Level_2').text();
                        var xml_Risk_Category_Level_3   = $(this).find('Risk_Category_Level_3').text();
                        var xml_GTO_Library_Ref_ID  = $(this).find('GTO_Library_Ref_ID').text();
                        var xml_Ent_Lib_Ref_ID      = $(this).find('Ent_Lib_Ref_ID').text();
                        var xml_Name            = $(this).find('Name').text();
                        var xml_Description     = $(this).find('Description').text();
                        var xml_Potential_Risk_Event    = $(this).find('Potential_Risk_Event').text();
                        var xml_Cause           = $(this).find('Cause').text();
                        var xml_GTO_Library     = $(this).find('GTO_Library').text();
                        var xml_Comments        = $(this).find('Comments').text();
                        // Add matched items to an array
                        xmlArr += '&lt;tr&gt;&lt;td&gt;';
                        xmlArr += xml_OAid;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_BAC_Level_1;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Basel_Level_1;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Basel_Level_2;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Risk_Category_Level_3;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_GTO_Library_Ref_ID;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Ent_Lib_Ref_ID;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Name;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Description;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Potential_Risk_Event;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Cause;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_GTO_Library;
                        xmlArr += '&lt;/td&gt;&lt;td&gt;';
                        xmlArr += xml_Comments;
                        xmlArr += '&lt;/td&gt;&lt;/tr&gt;';

                        //alert(xmlArr);
                        }
                      }); // end each loop

                  //Append array to table (this way is much faster than doing this individually for each item)
                  //alert(xmlArr)
          $(xmlArr).appendTo("tbody#oa");
        } // end post AJAX call operaitons
        }); // end AJAX
            }

    );
    }
    &lt;/script&gt;
</code></pre>
