Reputation: 167
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;
}
Upvotes: 0
Views: 1273
Reputation: 955
Since you're generating the HTML for the table directly, I'd suggest that you put the class="className"
value directly into the HTML string rather than setting element class with code: http://jsfiddle.net/tNVGJ/
If you insist on setting the class with jQuery, could you try this and let me know how it turns out? http://jsfiddle.net/tNVGJ/1/
setTimeout(function () {
$('#Master').addClass('CalendarTable');
$('#MasterDatesRow').addClass('DateHeader');
$('#HeaderCell').addClass('Header');
}, 1000);
Upvotes: 1