I am dynamically pasting some html into a webpage (after I detected an event from the user). This html needs some css style style, and I am wondering if this is the cleanest way to do this using jQuery. I am not a website developer, so I cannot add these rules to the original css.
Suppose I already inserted some html without styling, including the formElement class. I can write a new <style> block in my document, for example:
my_html = '<style type="text/css">'; my_html += ' .formElement {'; my_html += ' display: block;'; my_html += ' width: 240px;'; my_html += ' position: relative;'; my_html += ' padding: 4px 0;'; my_html += ' }'; my_html += '</style>'; my_html = $(my_html); $('body').prepend(my_html);
Or I can use the css method:
$('.formElement').css({ 'display': 'block', 'width': '240px', 'position': 'relative', 'padding': '4px 0' });
Which solution is best / clean?
EDIT:
Since I cannot directly add rules to CSS, I will stick with the jQuery css method. Some other related issues also provide solutions:
I cannot use jQuery plugins, but if I could, I would of course use jQuery.Rule
Thank you all for your precious help.
clement g
source share