I am creating part of a webpage using .append() to add elements to another generated parent element. I want to style the added elements, but none of the styles provided to them using .css() are applied, perhaps due to the fact that these elements have not yet been added to the page?
Here is my code:
var stickyHeader = $("<div></div>"); $(".stickyHeader").find("th").each(function() { var offset = $(this).offset().left; var objWidth = $(this).width(); stickyHeader.append("<span>" + $(this).html() + "</span>").css({width: objWidth, top: 0, left: offset, position: 'fixed'}); }); $("body").append("<div class=\"stickyHeader\">" + stickyHeader.html() + "</div>");
Do I need more .each() scroll span after they are added to body , or is there a more pleasant solution?
I forgot to mention that each span given a width from another element on the page, so this cannot be done only with CSS and classes.
source share