Add CSS rule via jQuery for future created elements
This should work:
var style = $('<style>.class { background-color: blue; }</style>');
$('html > head').append(style);
jQuery CSS() for dynamically created elements
There's no event for elements created (not universally available, anyway). You could
- Add the rules to a stylesheet so that they are automatically applied to the newly created elements
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");
Persistent css rule changes for dynamically created elements
You order javascript at execution time to add an inline style to all elements with class 'red', coloring them blue. After that you add new elements to the DOM. They have no knowledge of the earlier command an therefore are not influenced by them.
This solves your problem, as this adds a style rule to the document. This influences all elements and is 'persistent':
$( "<style>.red { background: blue; }</style>" ).appendTo( "body" );
See: https://jsfiddle.net/aLghL2ke/
Note that if you want to run this command multiple times, it would be nice to remove the old style block from your code. This can be done with jQuery too using this command:
$( "body style" ).remove();
See: https://jsfiddle.net/4duxsz0a/
Add CSS rule via jQuery for future created elements
This should work:
var style = $('<style>.class { background-color: blue; }</style>');
$('html > head').append(style);
jQuery hide / show class not changing css for future added elements
Look at this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="imagetoolbar" content="false">
<meta http-equiv="imagetoolbar" content="no">
<style type="text/css">
.classname {
color: red;
font-size: 14px;
}
</style>
<script type="text/javascript">
window.onload = function() {
document.getElementById("button").onclick = function() {
var ss = document.styleSheets;
for (var i=0; i<ss.length; i++) {
var rules = ss[i].cssRules || ss[i].rules;
for (var j=0; j<rules.length; j++) {
if (rules[j].selectorText === ".classname") {
rules[j].style.color = "green";
}
}
}
};
}
</script>
</head>
<body>
<h1 class="classname">Some red text</h1>
<button id="button">Make text green</button>
</body>
</html>
Related Topics
Programmatically Focus on Next Input Field in Mobile Safari
How to Run Background Tasks in React Native
Scraping JavaScript Website in R
Open a File with Default Program in Node-Webkit
Opera Preventdefault() on Keydown Event
How to Group Data with an Angular Filter
Handle Response - Syntaxerror: Unexpected End of Input When Using Mode: 'No-Cors'
Vuejs: Why Is "This" Undefined
Detect Click Outside React Component
How to Use Jquery with Node.Js
Objective-C/Swift Regex Matching Across Multliple Lines
Convert Dd-Mm-Yyyy String to Date
Run JavaScript Code on Window Close or Page Refresh
JavaScript Is Creating Date Wrong Month
Module.Exports VS Exports in Node.Js
Generating PDF Files with JavaScript