Change CSS Rule in Class Using Jquery

Change CSS rule in class using JQuery

jQuery.css will find all existing elements on the page that have the Foo class, and then set their inline style width to 40px.

In other words, this doesn't create or change a css rule -- if you dynamically add an element with the Foo class, it would still have a width of 20px, because its inline style hasn't been set to override the default CSS rule.

Instead, you should use addClass and removeClass and control the styles in your static CSS.

Change CSS class properties with jQuery

You can't change CSS properties directly with jQuery. But you can achieve the same effect in at least two ways.

Dynamically Load CSS from a File

function updateStyleSheet(filename) {
newstylesheet = "style_" + filename + ".css";

if ($("#dynamic_css").length == 0) {
$("head").append("<link>")
css = $("head").children(":last");

css.attr({
id: "dynamic_css",
rel: "stylesheet",
type: "text/css",
href: newstylesheet
});
} else {
$("#dynamic_css").attr("href",newstylesheet);
}
}

The example above is copied from:

  • How To Switch CSS Files On-The-Fly Using jQuery

Dynamically Add a Style Element

$("head").append('<style type="text/css"></style>');
var newStyleElement = $("head").children(':last');
newStyleElement.html('.red{background:green;}');

The example code is copied from this JSFiddle fiddle originally referenced by Alvaro in their comment.

How can I change the css class rules using jQuery?

As far as I know there's no jQuery way to do this. There might be some jQuery plugin for this but I don't know.

Basically, what you're trying to achieve in your first question is possible using the styleSheets property of the document object. It's a little bit more complicated than that as you need to walk to a rather deep object chain, but nevertheless works in all major browsers including Internet Explorer 6. Below is a proof of concept. The CSS is inside a STYLE tag, but works with external CSS just as well. I'll let you do the abstractions.

Proof of Concept

<!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>

For your second question, I don't have time to write a solution but it would involve reading the CSS declarations just as above and use the cssText property of a CssRule object in order to build a string which will eventually be sent to the server using a Ajax POST request. The server side is your business.

References:

  • document.styleSheets (Mozilla)
  • styleSheet object (Mozilla)
  • CssRule object (Mozilla)
  • document.styleSheets (MSDN)
  • CssRule object (MSDN)

Hope it helps

jQuery CSS: Dynamically change attributes of a class

Using the css() method changes the inline styles on already existing elements, and you can't use that to change the styles on future elements. A workaround (that I don't like very much) would be to insert a style tag:

$( "<style>.myClass {background-color : #00FFFF}</style>" ).appendTo( "head" )
$("p").last().after("<div class='myClass'>Now!</div>");

FIDDLE

Dynamically change CSS rules in JavaScript or jQuery

You jQuery .css() method to do that.

$('.red').css('color', 'purple');

For multiple rules:

$('.red').css({
'color': 'purple',
'font-size': '20px'
});

When you add dynamic element in future to DOM by the way of append, just give those element some class or id and write CSS rules like above after appending them and they will applied for all dynamically created element.

Working sample

Note

Add dynamic rules is not a good solution in my point of view. Instead of the you can load some external CSS file.

But if you need something like dynamic rules add method then:

$('head').append(
$('<style/>', {
id: 'mystyle',
html: '.red {color: purple }'
})
);

And for future use:

$('#mystyle').append(' .someother { color: green; font-size: 13px } ');

Working sample

Overwrite a css rule using javascript or jquery

You should not change a stylesheet rule. Instead change where you are adding classes to control the display state. Add a class to the table (or tbody) and not the rows.

Example: you want company rows hidden, you add a class "companyHidden" to the table element. The following rule will hide those rows.

table.companyHidden tr.company {
display: none;
}

This way you do not have to worry about new rows and CSS does the work for you.

how to switch between left and right css rules using Jquery?

You can try:

$('.dropdown-menu').css({
'right':'auto',
'left': '0'
});

Set the default value for right rule and the one you want for left



Related Topics



Leave a reply



Submit