So, I have an application where I have a blank table in the DOM and I load JSON table into the table dynamically with jQuery's .getJSON function. This works...most of the time.
Sometimes my table appears on the page without any data, although firebug says my table and the JSON where loaded.
Anyone have any ideas why this may happen?
Is it possible that the JSON is loading before the table (it shouldn't be)?
Would a setTimeout be helpful, to make sure the table is loaded before I try loading the JSON?
Code:
if (currentProdSelectValue === "Candy" && currentAppSelectValue === "Candy Store") {
$('#page_content').children().fadeOut('slow');
$('#page_content').load('page_eleven_new.html').hide().fadeIn('slow');
$.getJSON('js/page_eleven.js',
function(data){
$('table#table_first_row tr:first td:first').html(data.candy.dataDaily.timeScope);
$('table#table_first_row tr:first td:nth-child(2)').html(data.candy.dataDaily.lemonHmarsBarss);
$('table#table_first_row tr:first td:nth-child(3)').html(data.candy.dataDaily.snickers);
$('table#table_first_row tr:first td:nth-child(4)').html(data.candy.dataDaily.cottonCandy);
$('table#table_first_row tr:first td:nth-child(5)').html(data.candy.dataDaily.marsBars);
$('table#table_first_row tr:first td:nth-child(6)').html(data.candy.dataDaily.babyRuth);
$('table#table_first_row tr:first td:nth-child(7)').html(data.candy.dataDaily.nerds);
$('table#table_first_row tr:first td:nth-child(8)').html(data.candy.dataDaily.atomicFireBalls);
$('table#table_first_row tr:first td:nth-child(9)').html(data.candy.dataDaily.fruitStripeGum);
$('table#table_first_row tr:nth-child(2) td:first').html(data.candy.dataMonthToDate.timeScope);
$('table#table_first_row tr:nth-child(2) td:nth-child(2)').html(data.candy.dataMonthToDate.lemonHmarsBarss);
$('table#table_first_row tr:nth-child(2) td:nth-child(3)').html(data.candy.dataMonthToDate.snickers);
$('table#table_first_row tr:nth-child(2) td:nth-child(4)').html(data.candy.dataMonthToDate.cottonCandy);
$('table#table_first_row tr:nth-child(2) td:nth-child(5)').html(data.candy.dataMonthToDate.marsBars);
$('table#table_first_row tr:nth-child(2) td:nth-child(6)').html(data.candy.dataMonthToDate.babyRuth);
$('table#table_first_row tr:nth-child(2) td:nth-child(7)').html(data.candy.dataMonthToDate.nerds);
$('table#table_first_row tr:nth-child(2) td:nth-child(8)').html(data.candy.dataMonthToDate.atomicFireBalls);
$('table#table_first_row tr:nth-child(2) td:nth-child(9)').html(data.candy.dataMonthToDate.fruitStripeGum);
//table row three
$('table#table_first_row tr:last td:first').html(data.candy.dataYearToDate.timeScope);
$('table#table_first_row tr:last td:nth-child(2)').html(data.candy.dataYearToDate.lemonHmarsBarss);
$('table#table_first_row tr:last td:nth-child(3)').html(data.candy.dataYearToDate.snickers);
$('table#table_first_row tr:last td:nth-child(4)').html(data.candy.dataYearToDate.cottonCandy);
$('table#table_first_row tr:last td:nth-child(5)').html(data.candy.dataYearToDate.marsBars);
$('table#table_first_row tr:last td:nth-child(6)').html(data.candy.dataYearToDate.babyRuth);
$('table#table_first_row tr:last td:nth-child(7)').html(data.candy.dataYearToDate.nerds);
$('table#table_first_row tr:last td:nth-child(8)').html(data.candy.dataYearToDate.atomicFireBalls);
$('table#table_first_row tr:last td:nth-child(9)').html(data.candy.dataYearToDate.fruitStripeGum);
});
}