0

If I have an element -

<div class="myDiv"></div>

myDiv has some rules applied to it -

.myDiv {
    background:red;
}

I then apply another class to myDiv -

$('.myDiv').addClass('newClass');

There's a definition in my css like this -

.myDiv.newClass{
    position:relative;
    left:20px;
}

How can I find out what rules apply specifically to $('.myDiv.newClass')? I would like a function that returns an object with the css rules in.

The result would be something like this -

{
    "position":"relative",
    "left":"20px"
}
Finnnn
  • 3,530
  • 6
  • 46
  • 69
  • http://stackoverflow.com/questions/1004475/jquery-css-plugin-that-returns-computed-style-of-element-to-pseudo-clone-that-el – Dave Jun 18 '12 at 11:34

3 Answers3

0

I do not want return any rules that apply only to .myDiv

In which case you should remove this class from your element and change your CSS structure:

$('.myDiv').addClass('newClass');    
$('.myDiv').removeClass('myDiv');

Then change:

.myDiv.newClass {}

to:

.newClass {}

If you need to see what styles are now being applied to that div, you can "Inspect Element" using Google Chrome or FireBug.

Community
  • 1
  • 1
Curtis
  • 101,612
  • 66
  • 270
  • 352
  • 1
    Does `$('.myDiv').addClass('newClass');` really works ? You don't have `myDiv` class anymore at this point. – zessx Jun 18 '12 at 12:05
  • Thanks for your answer, I was unclear in my question. I've edited to show what I'd like returned. – Finnnn Jun 18 '12 at 12:53
0

window.getComputedStyle() maybe what you are looking for - https://developer.mozilla.org/en/DOM/window.getComputedStyle

techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

a very nice article about fetching the styles - browser compatible

Quirks

The above given like is a generalised solution to your problem.

Vivek Chandra
  • 4,240
  • 9
  • 33
  • 38