views:

43

answers:

3

Hi Guys,

I have the follow HTML

<div>This is some <span>special <a href="#">text</a></span> and it's super</div>

And CSS

span {color:#333;}
a {color:#777;}
a:hover {color:#AAA;}

I am wondering what I can use to setup a function that I can extract the color of the <a> and <a>:hover elements?

Thanks

A: 

Have a look at the .css documentation here

Fiona Holder
hey just wondering - how would I do this for multiple elements ? i.e.var theColorIs = $('a').css("color", "font-size"); as that doesn't seem to work ?
Tom
+1  A: 

To get the color CSS attribute of all the elements you can use JQuery's css() function:

$('a').each(function(index) {
               alert( $(this).css('color') );
             });​​​​

This will iterate through all the anchor elements on the page and tell you the CSS color attribute thereof.

Josiah
hey just wondering - how would I do this for multiple elements ? i.e.var theColorIs = $('a').css("color", "font-size"); as that doesn't seem to work ?
Tom
If you pass a second argument to css() it will set the css attribute specified. For example, to change the color to green you could say:$('a').css('color','green')If you want to access multiple CSS properties of an element, you'll have to make the call to css() multiple times:$('a').css('color);$('a').css('font-size');For more info, read the documentation posted in the other answer :)
Josiah
A: 

Use the .css() selector on the element you want to retrieve.

In your example:

var theColorIs = $('a').css("color");
RPM1984
oh great thanks. ill check it out
Tom
nps. dont take my example literally though - my example would actually return a collection of 'a' elements. just modify it to your use.
RPM1984
hey just wondering - how would I do this for multiple elements ? i.e.var theColorIs = $('a').css("color", "font-size"); as that doesn't seem to work ?
Tom
Check the .css link. The second parameter is what to set the attribute to - in that case you cant set the "color" attribute to the value of "font-size".Use the "each" function (like described by Josiah below) to iterate over multiple elements.
RPM1984