-1

http://jsfiddle.net/g6h28a2a/

div {
    background: url('http://i.imgur.com/tOaDddv.png') no-repeat;
}
alert($('div').css('background'));

I expect it to return url('http://i.imgur.com/tOaDddv.png') no-repeat; but it returned a strange string. Although it worked too if I assign that value to another element but I just don't understand why it returned that strange string.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
James Lemon
  • 426
  • 5
  • 17

3 Answers3

3

'background' is a concatenation of properties (read more about this on MDN). What I asume you want is the 'background-image' and 'background-repeat' properties.

alert($('div').css('background-image') + " " + $('div').css('background-repeat'));
Serf
  • 51
  • 3
  • 1
    Ya but what if as suggested by OP in other answer's comment there is an `!important` statement. In fact OP should look at `document.styleSheets` but i just don't see any reason for that – A. Wolff Apr 02 '15 at 13:11
  • @A.Wolff: That functionality does not exist in jQuery. Check http://stackoverflow.com/questions/10314131/check-if-css-property-has-important-attribute-applied for a none jQuery solution. – Serf Apr 02 '15 at 13:46
2

you colud combine two attributes to get the same what you need:

alert(
   $('div').css('background-image') 
   + ' ' 
   + $('div').css('background-repeat') 
 );
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
Andriy B.
  • 421
  • 3
  • 14
-1

You can try

alert($('div').css('background-image'));
div {
    background: url('http://i.imgur.com/tOaDddv.png') no-repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div></div>
epascarello
  • 204,599
  • 20
  • 195
  • 236
Akshay
  • 14,138
  • 5
  • 46
  • 70