I have a table on my website which is created initially when the page is loaded. The source data is from an API and changes throughout the day, I added a button to refresh the data but when it's clicked the data in the table is duplicated. It only seems to update the table properly when the page is refreshed which isn't what I want.
Here is the code I have to create the table:
for (var i in api_data) {
var wrap = document.getElementById("live_data");
var new_row = '';
new_row += '<div id="id'+api_data[i].bf_event_id+'" class="table-row">';
new_row += '<div class="table-cell text-left"><div class="data-country text-bold">'+api_data[i].Country+'</div></div>';
new_row += '<div class="table-cell text-center"><div class="data-time text-bold">'+parseTime(api_data[i].Start_Time)+'</div></div>';
new_row += '<div class="table-cell text-left"><div class="data-variable text-bold">'+api_data[i].variable+'</div></div>';
new_row += '<div class="table-cell text-left"><div class="data-course text-bold">'+api_data[i].Course+'</div></div>';
new_row += '<div class="id table-cell text-bold">'+api_data[i].Name+'</div>';
new_row += '<div class="table-cell text-right"><div class="data-advised-odds text-bold">'+api_data[i].Odds+'</div></div>';
new_row += '<div class="table-cell text-right"><div class="data-edge text-bold">'+api_data[i].Edge+'</div></div>';
new_row += '<div class="table-cell text-right"><div class="data-edge text-bold">'+api_data[i].strategy_id+'</div></div>';
new_row += '</div>';
wrap.innerHTML += new_row;
}
This just sits within a function and is called when the page is refreshed or when the refresh button is clicked.
Is there a way to completely rebuild the table without duplicating the existing data? The data in each row is unique so I thought I could just check if the Name already exists before adding the new row but I don't know how to query the existing table?
thanks
wrapbefore the loop runs, which will create the new rows.