0

I change with setAttribute('color', 'black') the CSS of some element. After this element will be stored in a jQuery.data() object. But in my data() object the CSS which I defined before won't be stored.

What am I doing wrong?

Thanks for the help!

2
  • Can you show us your example code ? perhaps on jsfiddle.net Commented Oct 13, 2011 at 13:50
  • 1
    setting an attribute is not the same as setting the CSS. What you want is the .css() function, not .setAttribute(). Commented Oct 13, 2011 at 13:59

2 Answers 2

3

If I have:

<div class="element"></div>

With jquery you can do:

$(".element").css("color", "black");

This will render the following:

<div class="element" style="color:black;"></div>

However using setAttribute will render:

<div class="element" color="black"></div>
Sign up to request clarification or add additional context in comments.

Comments

2

setAttribute does not alter CSS. It alters attributes.

If you want to set CSS then you would:

element.style.color = 'black';

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.