What I have is a number of divs, belonging to several categories.
These divs come and go. The color of a div should be determined by the
color defined for the category they belong to; category colors change
as well.

Say we're dealing with just a single category, colored red. One div
might be present:
<div class="cat_1"/>

We use the command you used to set the color to red $('.cat_1').css
('color', '#ff0000');
With this method, when another div belonging to the same category is
added, the command will have to be called again. Or a div changes
category, which can happen, the command will have to be called again.

The problem is that the .css() function merely sets the style
attribute of every element matched. Instead would like to change the
stylesheet definition for '.cat_1', requiring only one call (when a
category color is set).

On Mar 30, 8:32 pm, James <james.gp....@gmail.com> wrote:
> Documentation:http://docs.jquery.com/CSS
>
> $("#myElem").css('color', '#ff0000');
>
> It would probably be better if 'cat_[id]' is the element's ID rather
> than a CLASS, provided that it's unique in the whole document:
> <div id="cat_12">...</div>
>
> Using that, you can easily update a whole set of 'cat_[id]' in one
> call (if that's what you want to do):
> $("[id^=cat_]").css('color', '#ff0000');
> This will update every element with IDs that begin with 'cat_'.
>
> Theoretically you can do that with CLASS also, but when the CLASS
> attribute also allows definition of multipleclasses, such as:
> class="header cat_45"
> in which case would require a bit more work to locate because it would
> not fall in the criteria of "begins with cat_" anymore.
>
> On Mar 29, 4:23 am, Lars Schöning <tahp...@gmail.com> wrote:
>
> > Hello,
>
> > Apologies if this turns out to be a trivial question. I currently am
> > working withjQueryon a set of elements assigned to colored
> > categories via a 'cat_[id]' class. Categories can change colors and if
> > they do I want to update the color of every elements belonging to it.
> > I want the same to be true in all new events added.
>
> > I would believe that the easiest solution to this problem would be
> > changing the 'color' attribute directly in theCSSclass 'cat_[id]',
> > but have found no way to do so. As I am fairly new tojQueryI might
> > also be overseeing a much more elegant solution. Can anyone clarify?
>
> > Thanks in advance,
> > Lars

Reply via email to