I'm using jQuery CSS function to style some elements
$element.css(style);
This works, but a part of the elements are created dynamically after the page load. This should be
$element.live ('created',function() {
$(this).css(style);
});
I'm stuck on the created event. Any ideas?
The better way is to add css classes which you want to add and then use addClass to add css dynamically.
The css() method is used to change style property of the selected element.
Use another CSS class selector *="class" (equivalent to CSS attribute selector) which select all elements whose class contains at least one substring "box-".
In this article, we will see how to dynamically create a CSS class and apply to the element dynamically using JavaScript. To do that, first we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList property in JavaScript.
There's no event for elements created (not universally available, anyway). You could
Chain the css() method when you create your elements:
$('<img id="createdImage" src="some.jpg"/>')
.appendTo(document.body)
.css(style);
Create a new stylesheet dynamically:
$("<style>").text("#myNewEl { width:20px; height:30px; }").appendTo("head");
Best Idea is to Define CSS classes. And then Remove and add classes from Dynamic elements as per need
$(element).addClass("className");
$(element).removeClass("className");
Example: JS Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With