tags:

views:

84

answers:

3

Hello, I'm writing a jQuery plugin and something I need to be able to do is determine the width of an element that the user specifies. The problem is that .width() or .css('width') will always report exact pixels, even if the developer has assigned it e.g. width:90% with CSS.

Is there any way to have jQuery output the width of an element in px or % depending on what the developer has given it with CSS?

+8  A: 

I'd say the best way is to compute it yourself:

var width = $('#someElt').width();
var parentWidth = $('#someElt').offsetParent().width();
var percent = 100*width/parentWidth;
Matt Ball
@downvoter: any comments for me?
Matt Ball
Thanks, that would certainly work, but I wanted to have the script automatically determine whether the developer specified a percent or pixel, which I don't think can be done so I'll just add it as an option that they can pass to the plugin.
joren
@joren - Please revise your question title. It contradicts the meaning of your actual question.
Peter Ajtai
@joren: I saw that you revised your question title. Do you have further questions?
Matt Ball
Not really, I still ended up deciding to just add setting the percent of the div as an option to my plugin, it seemed the best course of action after this discussion. Thanks!
joren
+1  A: 

I think you can use stylesheet ('style') object access directly. Even then, YMMV by browser. e.g. elm.style.width.

Edit for Peter's comment:: I am not looking to 'get a %'. As per the question:

I need to be able to do is determine the width of an element that the user specifies ... [is there a way to] output the width of an element in px or % depending on what the developer has given it with CSS?

Thus I provided an alternative to retrieve the "raw" CSS value. This appears to work on FF3.6. YMMV elsewhere (netadictos's answer may be more portable/universal, I do not know). Again, it is not looking to 'get a %'.

pst
How does this return a percent?
Peter Ajtai
I see. I was going off the title: `Getting jQuery .width() to output percentage instead of px?`. The question body contradicts the title. It's unclear what the OP wants.
Peter Ajtai
+1  A: 

The way to do it is documented in this stackoverflow question.

http://stackoverflow.com/questions/324486/how-do-you-read-css-rule-values-with-javascript

The only thing you have to know is in which stylesheet is the class or loop through all the stylesheets. The following function gives you all the features of the class, it would be easy to do a regex to extract the feature you look for.

function getStyle(className) {
    var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules
    for(var x=0;x<classes.length;x++) {

        if(classes[x].selectorText==className) {
                (classes[x].cssText) ? alert(classes[x].cssText) : alert(classes[x].style.cssText);
        }
    }
}
getStyle('.test');
netadictos
How does this return a percent if the orginal style is either not in a percent or it is not defined?
Peter Ajtai
The question is: "Is there any way to have jQuery output the width of an element in px or % depending on what the developer has given it with CSS?", that is to say the user is asking to ouput what is in the css.
netadictos
I see. I was going off the title: `Getting jQuery .width() to output percentage instead of px?`. The question body contradicts the title. It's unclear what the OP wants.
Peter Ajtai