17

I am reading the book "jQuery Pocket Reference", O’Reilly, 2011.

On page 15 it says:

'For example, calling attr("css", {backgroundColor:"gray"}) is the same as calling css({backgroundColor:"gray"}).'

But I cannot make the attr(“css”, { }) work. My test code: http://jsfiddle.net/4UMN3/4/

$(function() {
$("#btnChangeColor").click(function () {
$("#spanText").attr("css", { backgroundColor: "gray" });

 });

});

The css() method works fine, http://jsfiddle.net/4UMN3/5/

jkdev
  • 11,360
  • 15
  • 54
  • 77
user3026964
  • 215
  • 1
  • 4
  • 7
  • 4
    Nothing about it in [the docs](http://api.jquery.com/attr/). Might be an [error in the book](http://www.oreilly.com/catalog/errata.csp?isbn=0636920016182). – Blazemonger Jul 10 '14 at 22:38
  • 8
    Why would you need to use `.attr()` if you have `.css()`? – j08691 Jul 10 '14 at 22:38
  • 2
    Does this correspond to the [second confirmed error on O'Reilly's site](http://www.oreilly.com/catalog/errata.csp?isbn=0636920016182)? – Blazemonger Jul 10 '14 at 22:43
  • Yes, the css() works fine. It's just because I am reading this book. In develop tool, I found css="[object Object]" was added to the span. It seems that the attr() cannot recognize the “css” attribute. This is a test – user3026964 Jul 10 '14 at 22:45
  • 2
    @Blazemonger - Good find, that's the one. Author goofed. – j08691 Jul 10 '14 at 22:48
  • 1
    Yup, the book is definitely wrong – Alnitak Jul 10 '14 at 22:56
  • 1
    The attribute is not css but style, but, in fact, this is not the same thing that using css(). If you read it in a book, this is a mistake. – Loenix Jul 10 '14 at 23:02
  • 1
    @j08691 to be sure, the author also submitted the correction. :-) – Blazemonger Jul 10 '14 at 23:41

7 Answers7

28

Replace:

$("#spanText").attr("css", { backgroundColor: "gray" });

with

$("#spanText").attr('style',  'background-color:gray');
Branimir Đurek
  • 632
  • 5
  • 13
9

Probably, it was meant to be

$("#spanText").attr('style', 'background-color:gray');

This may work, but has some problems:

  • It is preferred to change style property instead of style attribute.
  • It will replace all previously set inline styles.

Then, if you use jQuery, better use css method:

$("#spanText").css('background-color', 'gray');

But style property is useful in vanilla-js:

document.getElementById("spanText").style.backgroundColor = 'gray';
Oriol
  • 274,082
  • 63
  • 437
  • 513
3

I think jQuery's .attr() can only affect attributes the element has. HTMLElements do not know an attribute called "css" (meaning <div css="something"></div>).

So, the correct usage would be

$("#spanText").attr("style",{backgroundColor:"gray"});

But this outputs

<span id="spanText" style="[object Object]">This is a test</span>

An example that actually works is

$("#spanText").attr("style","background-color:gray;");
SVSchmidt
  • 6,269
  • 2
  • 26
  • 37
1

$("#id").attr('style',  'color:red;cursor:pointer');
Kanomdook
  • 715
  • 8
  • 6
0

=> the .css method modifies the style attribute not the "css" .

$().att('style' , 'backgroundColor : gray');

=> there is no such thing as css attribute in html

user117291
  • 23
  • 4
0

you use jQuery so you have to use css and not attr, 'cause css add property but attr will replace all style if existe !

karim somai
  • 129
  • 1
  • 3
0
<style>
    .newClass{
    color:#fff;
    }
    .test2{
    color:red;
    }
    .newclass{
    color:blue;
    }
</style>

<script>
$(document).ready(function(){

    //get style and class name in alert
      $('#attr').click(function () {
        alert($('h3').attr("style"));
        alert($('h3').attr("class"));
      });

     //set css property
     $("#setcss").click(function(){
        $("#test").css({"background-color": "#000", "color": "teal"});
      });

      //change class name property
      $('#changeclassname').click(function(){
        $('#test3').removeClass('test2').addClass('newclass');
      });
});


</script>


<!--get style and class name in alert-->
<h3 class="test" style="color: white; background: red;">This is the tag and this will be our tag</h3> 

<button id="attr">get Attribute</button>


<!--set css property-->
<p id="test" class="test1">This is some <b>bold</b> text in a paragraph.</p>

<button id="setcss">set Attribute</button>


<!--change class name property-->
<p id="test3" class="test2">This is some text in a paragraph.</p>

<button id="changeclassname">set Attribute</button>
jignasha
  • 168
  • 3