2

I know that Attr gets the exact class of an element. Is there a way to get the element exact class as defined in CSS? For example an element may have class "title" but be defined in CSS as a descendant selector, something like: .other-list .title {} I need something to get that exact class value. You can see here the full example: http://jsfiddle.net/2TMgQ/2/

On click both elements have class "title" but are defined in CSS as 2 different elements. Thank you

10
  • You can do this, but do you need to? It's possible, Firebug Lite does this, but it isn't pretty: fbug.googlecode.com/svn/lite/branches/firebug1.4/content/… Commented Jul 6, 2010 at 20:59
  • YEap, I need to. Indeed Firebug does it. I do not think that i will be able to understand the FireBug code. That was the first place i had looked in to, I thought that there is an easier way... how about this: developer.mozilla.org/en/DOM/cssRule.selectorText Commented Jul 6, 2010 at 21:06
  • Indeed they are using "selectorText", just looked again to their code. Can't figure it out how, the code is to big to understand it Commented Jul 6, 2010 at 21:12
  • 2
    @Mircea - Let me put it this way...the Firebug guys are good, and they wouldn't do it a way that's more complicated that was required (for them). You can strip from their implementation because you don't need all that functionality, but just finding the rules isn't easy. For example what if 10 rules applied to your element, then what? Which do you use? It could always be a list, what if your example was this instead? jsfiddle.net/2TMgQ/9 Commented Jul 6, 2010 at 21:18
  • 1
    @Marko - Why were the plates in the glasses? Was it really big glasses or really little plates? Commented Jul 6, 2010 at 21:42

3 Answers 3

1

This should get you all CSS styles that apply to the element. It uses jQuery's is function to check if a CSS rule selector applies to a given element. Iterate through all stylesheets in document.styleSheets, and through all cssRules in each stylesheet, and check if selectorText matches the element. In pseudocode:

given::element

matchedRules = []

for styleSheet in document.styleSheets
    for rule in styleSheet.cssRules
        if element.is(rule.selectorText)
            matchedRules.push(rule.selectorText)

return matchedRules;

In code:

function getCSSRules(element) {
    element = $(element);

    var styleSheets = document.styleSheets;
    var matchedRules = [], rules, rule;

    for(var i = 0; i < styleSheets.length; i++) {
        rules = styleSheets[i].cssRules;

        for(var j = 0; j < rules.length; j++) {
            rule = rules[j];
            if(element.is(rule.selectorText)) {
               matchedRules.push(rule.selectorText);
            }
        }
    }

    return matchedRules;
}

This should return all rules that apply to the element. For your first example, it returns:

[".links-list .title"]

For Nick's extended example, it returns:

[".links-list .title", ".title", "a", "li a"]

How you define specificity and how to decide what to filter out from this list is upto you.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the exact result I need. Let me try it out.
Thanx, this is getting the exact selectors as Firebug.
0

If you are referring to parent - child selectors then the syntax would be something like:

$(".other-list > .title")

Have a look at http://api.jquery.com/category/selectors/

1 Comment

He wants to GET the parent class
0

Maybe this could help

CSS parser/abstracter? How to convert stylesheet into object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.