-1

I'm trying to pull a css value from a certain class on a page, so that i can assign that to other elements in various ways.

I have set up an example in jsfiddle, here I would like to somehow grab the background from

.box

and add it to the "bg" variable

http://jsfiddle.net/43kt7/

  • add a dual class one specific to it and one shared – Dev Man Apr 24 '14 at 22:07
  • I don't think this could work for me, as I would have the background color in a shared class, but what if I wanted that color to be a border color on another element? – Karl Newark Apr 24 '14 at 22:10
  • you dont need jquery for this use see this and tell me if this is what you want http://jsfiddle.net/yasinallana/43kt7/1/ – Dev Man Apr 24 '14 at 22:10
  • 1
    There are few questions in SO: http://stackoverflow.com/questions/4493449/copy-all-styles-from-one-element-to-another – http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element/6416527#6416527 – doup Apr 24 '14 at 22:15
  • possible duplicate of [jquery: how to get the background color code of an element?](http://stackoverflow.com/questions/5999209/jquery-how-to-get-the-background-color-code-of-an-element) – pennstatephil Apr 24 '14 at 22:16
  • If you don't want to use jQuery, see https://developer.mozilla.org/en-US/docs/Web/API/window.getComputedStyle – Jahed Apr 24 '14 at 22:19

1 Answers1

1

Getting a specific css value using jQuery is done use the css() function. To get the background-color value, you can use:

var bg = $( ".box" ).css( "background-color" );
Ben Roux
  • 7,308
  • 1
  • 18
  • 21