I am using JQuery in a separate js file in a RAZOR MVC application to dynamically build a html table from scratch using the APPEND method. This works fine, however when I use ADDCLASS it seems as though none of the css is applied until after I refresh the page. Sometimes not at all! This code is a simple table with 30 day header cells and another row below with one cell spanned all across. I also included my css.
$(document).ready(function () {
var startDate = $.telerik.formatString('{0:d}', $("#Start").data("tDatePicker").value());
var forCalendarStartDate = new Date(startDate);
$('#CalendarWrapper').append('<table id="Master"></table>');
$('#Master').append('<tr id="MasterDatesRow"></tr>');
for (var i = 0; i < 31; i++) {
var headerDate = new Date();
var d = new Date(headerDate.setDate(forCalendarStartDate.getDate() + i));
var s = $.datepicker.formatDate('mm-dd', d);
$('#MasterDatesRow').append('<th>' + s + '</th>');
}
$('#Master').append('<tr id="Header1"></tr>');
$('#HeaderUSARAF').append('<td colspan="31" id="HeaderCell">USARAF</td>');
//Class
$('#Master').addClass('CalendarTable');
$('#MasterDatesRow').addClass('DateHeader');
$('#HeaderCell').addClass('Header');
});
css
.CalendarTable
{
border:1;
width:100%;
font-size:small;
}
.DateHeader
{
background-color:Green;
}
.Header
{
background-color:#DBACBC;
text-align:center;
}
CalendarWrapperelement is before$(document).ready(function()is getting called