Skip to content

Instantly share code, notes, and snippets.

@corysimmons
Forked from yckart/jquery.addrule.js
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corysimmons/10949352 to your computer and use it in GitHub Desktop.
Save corysimmons/10949352 to your computer and use it in GitHub Desktop.
/*!
* jquery.addrule.js 0.0.15 - https://gist.github.com/yckart/5563717/
* Add css-rules to an existing stylesheet.
*
* @see http://stackoverflow.com/a/16507264/1250044
*
* Copyright (c) 2013 Yannick Albert (http://yckart.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
* 2013/05/12
**/
(function($) {
window.addRule = function(selector, styles, sheet) {
if(typeof styles !== 'string') {
var clone = '';
for(var style in styles) {
var val = styles[style];
style = style.replace(/([A-Z])/g, '-$1').toLowerCase();
clone += style + ':' + (style === 'content' ? '"' + val + '"' : val) + '; ';
}
styles = clone;
}
sheet = sheet || document.styleSheets[document.styleSheets.length - 1];
if(sheet.insertRule) sheet.insertRule(selector + ' {' + styles + '}', sheet.cssRules.length);
else if(sheet.addRule) sheet.addRule(selector, styles);
sheet.addRule(selector, styles);
return this;
};
if($) {
$.fn.addRule = function(styles, sheet) {
addRule(this.selector, styles, sheet);
return this;
};
}
}(window.jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment