Using jQuery attr() to set “css”

attr, css, jquery

Solution

Replace:

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

with

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

Problem

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/

Original source