0

http://jsfiddle.net/EBPg9/2/

I'm working on a little project where I have multiple multicolored items, and tags within them. Instead of specifying my color for both text and background, I'd like to just get the background color of my parent div and assign it to the color of my text.

My current jQuery looks like this:

$(".infobox").each(function() {
     var tagcolor =    $(this).closest('.info').css('background');
     $(this).css('color',tagcolor);
     alert(tagcolor);
});

My HTML is

<div class="info" style="background: #408FCE;">
    <div class="infobox">
        tags
    </div>
</div>

As you can see from my jQuery, I created an alert that will show me the value of the variable "tagcolor", and weirdly it seems to be getting all other unwanted CSS values, even though I specified "Background". I can't figure out what I'm doing wrong, how can I fix this?

EDIT: Did some reseach here;

What is the difference between background and background-color

Apparantly "background" returns a bunch of different stuff.

Community
  • 1
  • 1
Alexander Lozada
  • 4,019
  • 3
  • 19
  • 41

8 Answers8

1

You can use "background" property to apply a bunch of values regarding the background such as:

background: #123456 url('http://www.path.to.your.image.com') left top no-repeat;

It's much shorter than:

background-color:#123456;
background-image:'http://www.path.to.your.image.com';
background-position:left top;
background-repeat:no-repeat;

When you retrieve the 'background' property, you get all of the values (default if not specified). Instead just query for the css "background-color" property.

Ronen Cypis
  • 21,182
  • 1
  • 20
  • 25
0

You need to use background-color instead of background

$(".infobox").each(function() {
     var tagcolor =    $(this).closest('.info').css('background-color');
     $(this).css('color',tagcolor);
     alert(tagcolor);
});

Fiddle Demo

Retrieval of shorthand CSS properties (e.g., margin, background, border), although functional with some browsers, is not guaranteed. (Taken from : http://api.jquery.com/css/)

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Fiddle Demo

use background-color instead of background

var tagcolor = $(this).closest('.info').css('background-color');

see all properties of background

.css('background-color') return all background properties that are set by ur css and browser

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

Use background-color instead and it will work.

$(".infobox").each(function() {
     var tagcolor =    $(this).closest('.info').css('background-color');
     $(this).css('color',tagcolor);
     alert(tagcolor);
});

<div class="info" style="background-color: #408FCE;">
    <div class="infobox">
        tags
    </div>
</div>
berentrom
  • 505
  • 1
  • 5
  • 12
0

try this please:

$(".infobox").each(function() {
     var tagcolor =    $(this).closest('.info').css('backgroundColor');
     $(this).css('color',tagcolor);
     alert(tagcolor);
});
Popo
  • 2,402
  • 5
  • 33
  • 55
  • 1
    background is a kinda compound css style, as backgroundColor is the specific attribute for the color – Popo Feb 16 '14 at 15:01
  • 1
    @AlexanderLozada `background` is a shorthand method that sets different properties, one of which is `background-color`. Therefore you can't set the value of `color` to the value of `background`. – Mr Lister Feb 16 '14 at 15:01
0

background contains a lot more than just the color!

Try specifying background-color:

$(".infobox").each(function() {
     var tagcolor =    $(this).closest('.info').css('background-color');
     $(this).css('color',tagcolor);
     alert(tagcolor);
});

DEMO

Yehuda Shapira
  • 8,460
  • 5
  • 44
  • 66
0

Try this

$('.infobox').each(function(index, element) {
        $(this).css({
            color : $(this).parent().css('background-color')
        })
    });

Demo

Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33
0

Try to use background-color instead of background

dongseok0
  • 737
  • 2
  • 7
  • 18