I have a solution for changing a value in a specific CSS class. But this only works if you keep your css in the tag. If you just keep a link to your css from external ex files.
<style src='script.js'></style>
this solution will not work.
If your css looks like this:
<style id='style'> .foo { height:50px; } </style>
You can change the tag value using JS / jQuery.
I wrote a function, maybe its not the best, but it works. You can improve it if you want.
function replaceClassProp(cl,prop,val){ if(!cl || !prop || !val){console.error('Wrong function arguments');return false;}
Then just change the tag variable to your style tag id and execute exegute:
replaceClassProp('foo','height','50px');
The difference between this and $ ('. Foo'). css ('height', '50px'); is that when you do this using the css jQuery method, all elements that have the .foo class will have a visible style = 'height: 50px' in the DOM. If you do it your way, the elements are untouched, and the only thing you will see is class = 'foo'
<strong> Benefits
- Clear DOM
- You can change the desired property without replacing the whole style.
disadvantages
- Inner CSS Only
- You must find the specific style tag you want to change.
Hope this helps.
wopolow Apr 17 '14 at 21:20 2014-04-17 21:20
source share