Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

jqLite .css() retrieves only inline styles #2866

Closed
redaemn opened this issue Jun 3, 2013 · 19 comments
Closed

jqLite .css() retrieves only inline styles #2866

redaemn opened this issue Jun 3, 2013 · 19 comments

Comments

@redaemn
Copy link

redaemn commented Jun 3, 2013

It seems that using jqLite .css() to retrieve styles define inside an external CSS file does not work. I tried this in Chrome 27

http://jsfiddle.net/redaemn/pbucv/1/

@btford btford closed this as completed Aug 24, 2013
@btford
Copy link
Contributor

btford commented Aug 24, 2013

As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.

Please try the newest versions of Angular (1.0.8 and 1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.

Thanks!

@redaemn
Copy link
Author

redaemn commented Aug 26, 2013

@btford I updated the fiddle to use Angular v1.0.8 and the issue persists

http://jsfiddle.net/redaemn/pbucv/10/

Here I also tried version 1.2.0rc1 and again the issue persists

I'd just like to know: is this a bug or is it supposed to work like this? (because jQuery original .css() doesn't work like this...)

Thanks for your help!

@deepak-nulu
Copy link

I would like this to work like in jQuery. i.e. jqLite's .css() method should return the values even if they are defined in an external CSS file. Without this functionality, I am now forced to include jQuery just for this. Thanks!

@btford btford reopened this Aug 27, 2013
@btford
Copy link
Contributor

btford commented Aug 27, 2013

This should either be documented, or fixed (not sure which).

@deepak-nulu
Copy link

Fixed, please :-)

@btford
Copy link
Contributor

btford commented Aug 27, 2013

Depends on how heavy the code is. The point of Angular having a jqLite is to avoid duplicating too much of jQuery. At some point, it's cheaper/easier to just include jQuery. Thus, I'm deferring the decision. :)

@deepak-nulu
Copy link

Makes sense. Thanks for the quick response.

@crtag
Copy link

crtag commented Nov 24, 2013

Then remove this method completely to avoid confusion OR clearly state this behaviour in docs. There's so little chance of inline styles so those who utilise that approach should really employ jQuery.

@caitp
Copy link
Contributor

caitp commented Nov 24, 2013

There's a line in one of my jqLite patches to add a call to getComputedStyle() to jqLite.css() --- It would be a simple thing to add that line in a separate patch as a quick fix to this, I really don't think it would hurt anything


Also, the currentStyle property (used if msie <= 8) does return not just the inline style, it's essentially getComputedStyle() --- so this only affects non-IE browsers, I guess (which does actually make the current jqLite.css() behaviour inconsistent across browsers)

@tomchentw
Copy link

Encounter same problem here. As @caitp said, I use getComputedStyle() to retrieve what I want here.
Refer to: https://github.com/jquery/jquery/blob/master/src/css/var/getStyles.js

@calummoore
Copy link

The solution is pretty much the same length!

Works in IE6 > and real browsers.

css: function(element, name, value) {
    name = camelCase(name);

    if (isDefined(value)) {
        element.style[name] = value;
    } else {
        var val;
        //for old IE
        if (isDefined(element.currentStyle)){
            val = element.currentStyle[name];
        }
        //for modern browsers
        else if (isDefined(window.getComputedStyle)){
            val = element.ownerDocument.defaultView
                .getComputedStyle(element,null)[name];
        }
        else {
            val = element.style[name];
        }
        return  (val === '') ? undefined : val;
    }

},

@howardengelhart
Copy link

+1

@charlie
Copy link

charlie commented Jan 22, 2014

+1 for @calummoore's solution

@tomchentw
Copy link

👍

1 similar comment
@seanzx85
Copy link

+1

@denkan
Copy link

denkan commented Jun 4, 2014

+1 @calummoore
Should be implemented.

@jmstout
Copy link

jmstout commented Jun 12, 2014

+1

1 similar comment
@heathkit
Copy link

+1

@Narretz
Copy link
Contributor

Narretz commented Sep 19, 2014

This will not be implemented as explained in this comment: #8161 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.