Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jQuery change css style definition? (not individual css of each element)

I haven't seen any docs saying jQuery can change any CSS definition such as changing

td { padding: 0.2em 1.2em } 

to

td { padding: 0.32em 2em } 

but either have to change a whole style sheet, or change class of each element, or change css of each element.

Is changing the style definition possible?

like image 802
nonopolarity Avatar asked Jul 02 '10 10:07

nonopolarity


People also ask

Can jQuery manipulate CSS?

The jQuery CSS methods allow you to manipulate CSS class or style properties of DOM elements. Use the selector to get the reference of an element(s) and then call jQuery css methods to edit it. Important DOM manipulation methods: css(), addClass(), hasClass(), removeClass(), toggleClass() etc.

Can we change CSS property using JavaScript or jQuery?

Finally, setting multiple css properties with jQuery can be done by passing in a javascript object. The key is name of the css property, and the value will be the css value.

Can JavaScript modify CSS?

With JavaScript, we are able to set CSS styles for one or multiple elements in the DOM, modify them, remove them or even change the whole stylesheet for all your page.

Which function in jQuery can be used to change the style of an element dynamically?

The css() method is used to change style property of the selected element. Here we have created two elements inside body tag i.e. <h1> and <h3> elements. We apply CSS property on <body> tag and <h1> tag using css() method dynamically.


2 Answers

$('head').append('<style id="customCSS">td { padding: 0.32em 2em }</style>'); 

I added an id attribute so you can target and remove it when you no longer want that change to apply.

$('#customCSS').remove(); 
like image 34
Michael Fitchett Avatar answered Sep 27 '22 21:09

Michael Fitchett


There is DOM access to stylesheets, but it's one of those things we tend to avoid because IE needs a load of compatibility cruft.

A better way would be typically be to trigger the change indirectly, using a simple class change on an ancestor:

td { padding: 0.2em 1.2em } body.changed td { padding: 0.32em 2em } 

Now just $('body').addClass('changed') and all the tds update.

If you really must frob the stylesheets:

var sheet= document.styleSheets[0]; var rules= 'cssRules' in sheet? sheet.cssRules : sheet.rules; // IE compatibility rules[0].style.padding= '0.32em 2em'; 

This assumes that the td rule in question is the first rule in the first stylesheet. If not, you might have to go searching for it by iterating the rules looking for the right selectorText. Or just add a new rule to the end, overriding the old one:

if ('insertRule' in sheet)     sheet.insertRule('td { padding: 0.32em 2em }', rules.length); else // IE compatibility     sheet.addRule('td', 'padding: 0.32em 2em', rules.length); 

jQuery itself doesn't give you any special tools to access stylesheets, but it's possible there are plugins that might.

like image 115
bobince Avatar answered Sep 27 '22 21:09

bobince