user3026964
user3026964

Reputation: 215

Using jQuery attr() to set “css”

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/

Upvotes: 17

Views: 100358

Answers (7)

jignasha
jignasha

Reputation: 176

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

Upvotes: 0

karim somai
karim somai

Reputation: 149

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

Upvotes: 0

Kanomdook
Kanomdook

Reputation: 735

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

Upvotes: 1

user117291
user117291

Reputation: 23

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

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

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

Upvotes: 0

Oriol
Oriol

Reputation: 288650

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

Upvotes: 9

Branimir Đurek
Branimir Đurek

Reputation: 632

Replace:

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

with

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

Upvotes: 28

SVSchmidt
SVSchmidt

Reputation: 6567

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

Upvotes: 3

Related Questions