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?
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"
You need to stack them up:
Dim existingClasses as string = ITEMID.Attributes("class")
ITEMID.Attributes.Add("class", existingClasses & " classToAdd")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With