2

I was trying to get the CSS rules for an HTML element.

I already found an answer here: Can jQuery get all CSS styles associated with an element?

The problem is that the given answer doesen't work, as it is stated in the comments; it gives me Uncaught Error: Syntax error, unrecognized expression: button.ui-button::-moz-focus-inner

The problem in that code is that it is passing every CSS rule selector to the jquery.is() method, that is probably unable to recognise 'every' CSS selector that can be written, so it gives an error on complicated rules.

I've searched around and looked at the standard but I'm running out of ideas.

There is another answer that gives the computed style, that is what I am using currently, but that is giving me so many useless properties that are damaging my result.

Any help appreciated!

Community
  • 1
  • 1
funforums
  • 326
  • 1
  • 12

1 Answers1

0

Resolved it this way: I modified the code from Can jQuery get all CSS styles associated with an element? to this one:

function css(a){
   var o = {};
   var rules = window.getMatchedCSSRules(a.get(0));
   for(var r in rules) {
         o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
   }
   return o;
}




function css2json(css){
        var s = {};
        if(!css) return s;
        if(css instanceof CSSStyleDeclaration) {
            for(var i in css) {
                if((css[i]).toLowerCase) {
                    s[(css[i]).toLowerCase()] = (css[css[i]]);
                }
            }
        } else if(typeof css == "string") {
            css = css.split("; ");          
            for (var i in css) {
                var l = css[i].split(": ");
                s[l[0].toLowerCase()] = (l[1]);
            };
        }
        return s;
    }
Community
  • 1
  • 1
funforums
  • 326
  • 1
  • 12