I have a little problem.
When I set the css rule using some property, say, background-image in an external .css file and then try to access it using javascript in another external .js file, it does not work. That is to say I do not get any value for document.getElementById(someId).style.backgroundImage.
But when I set the css rule using style attribute in html file itself, it works.
So, my query is can't I access css property in js, if css is set in external .css file.
Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");
Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section. External - by using a <link> element to link to an external CSS file.
You can only access style properties in Javascript that have been set via Javascript (or the style attr). To access the elements current style you should fetch the computed style of the element. var el = document. getElementById('hello'); var comp = el.
You can only access style properties in Javascript that have been set via Javascript (or the style
attr).
To access the elements current style you should fetch the computed style
of the element.
var el = document.getElementById('hello');
var comp = el.currentStyle || getComputedStyle(el, null);
alert( comp.backgroundColor );
Note, that the computed style may vary in browsers (like color being in hex or rgb) so you should normalize that if you want unified results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With