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/

8
  • 4
    Nothing about it in the docs. Might be an error in the book. Commented Jul 10, 2014 at 22:38
  • 8
    Why would you need to use .attr() if you have .css()? Commented Jul 10, 2014 at 22:38
  • 2
    Does this correspond to the second confirmed error on O'Reilly's site? Commented Jul 10, 2014 at 22:43
  • 2
    @Blazemonger - Good find, that's the one. Author goofed. Commented Jul 10, 2014 at 22:48
  • 1
    Yup, the book is definitely wrong Commented Jul 10, 2014 at 22:56

7 Answers 7

28

Replace:

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

with

$("#spanText").attr('style',  'background-color:gray');
Sign up to request clarification or add additional context in comments.

Comments

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';

Comments

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;");

3 Comments

The .css() function understands the object literal syntax, the .attr() method doesn't.
I never claimed something different. I just wanted to show why the shown syntax does not work.
I was just commenting for the benefit of any reader - not criticising your answer.
1

$("#id").attr('style',  'color:red;cursor:pointer');

Comments

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

Comments

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 !

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.