get CSS rule's percentage value in jQuery

css, jquery

Solution

There's no built-in way, I'm afraid. You can do something like this:

var width = ( 100 * parseFloat($('.largeField').css('width')) / parseFloat($('.largeField').parent().css('width')) ) + '%';

Problem

Let's say the rule is as follows: ``` .largeField { width: 65%; } ``` Is there a way to get '65%' back somehow, and not the pixel value? Thanks. EDIT: Unfortunately using DOM methods is unreliable in my case, as I have a stylesheet which imports other stylesheets, and as a result the cssRules parameter ends up with either null or undefined value. This approach, however, would work in most straightforward cases (one stylesheet, multiple separate stylesheet declarations inside the head tag of the document).

Original source