jQuery: The Write Less, Do More JavaScript Library

Talk:Attributes/attr

From jQuery JavaScript Library

Jump to: navigation, search

I noticed a bug when using the attr(properties) method.

If I do this:

$(".txlist").attr({class: "txlist on"});

It breaks IE7, Safari and FF 3

Instead I have to do this:

$(".txlist").attr("class", "txlist on");

It appears "class" is a reserved word for those browsers.

No, it's not a bug. In Javascript, CSS attributes don't always have a corresponding Javascript property with the same name. In your first example, class is simply incorrect. The correct property is className. In your second example, "class" is correct because that's the name of the CSS attribute, and when you use quotes jQuery wants the CSS attribute name. Another example of this is pixelTop which corresponds to top TheBilly 11:35, 21 April 2008 (PDT)