Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically remove specific class name from HTML element

Tags:

c#

.net

asp.net

In C# ASP.NET I am adding a class to an <input> using:

myInput.Attributes.Add("class","myClass");

At some point I would like to remove the added class (not all classes). Is there an equivalent along the lines of:

myInput.Attributes.Remove("class","myClass");

.Remove() seems to only accept a key (no pair value). Thank you!

like image 631
Jesse Avatar asked Dec 17 '22 00:12

Jesse


1 Answers

There is nothing built in to manage multiple values in the attribute.

You will have to parse the list, remove the class name and update the attribute.

Something like (untested):

var classes = myInput.Attributes["class"].Split(' ');

var updated = classes.Where(x => x != "classtoremove").ToArray();

myInput.Attributes["class"] = string.Join(" ", updated);
like image 75
Oded Avatar answered Dec 21 '22 22:12

Oded