Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes.Add("class", "className") but preserve existing class

Tags:

css

class

vb.net

Simple thing, well, I think it is.

I need to Add a class to an element within an asp:repeater under certain conditions, using VB.

So, I can do

ITEMID.Attributes.Add("class", "classToAdd")

But this removes the existing classes and therefore screws up my CSS.

ITEMID.Attributes("class") = "classToAdd"

Seems to do the same thing.

How do I add a class to an element, whilst preserving it's existing class values?

like image 219
Jamie Hartnoll Avatar asked Nov 01 '11 15:11

Jamie Hartnoll


2 Answers

Use += to add additional class, and make sure you leave a space before or else it will appear as currentClassclassToAdd, where current class is currentClass:

ITEMID.Attributes("class") += " classToAdd"

This is the same as doing:

ITEMID.Attributes("class") = ITEMID.Attributes("class") + " classToAdd"

Therefore:

ITEMID.Attributes("class") = "currentClass" + " classToAdd"
like image 186
Curtis Avatar answered Nov 16 '22 01:11

Curtis


You need to stack them up:

Dim existingClasses as string = ITEMID.Attributes("class")

ITEMID.Attributes.Add("class", existingClasses & " classToAdd")
like image 39
Joel Etherton Avatar answered Nov 16 '22 02:11

Joel Etherton