I need to remove a CSS class from an text field since I want to change its background color. I need to change the color back later, so I have to add and remove classes to the field, depending on some data.
.addClass
is working totally fine. The problem is that the original class is above the added, so the changes are not shown, but the class is added properly. I now try to remove the class which is above the added - but I cant remove any classes from my elements. Why is that? What am I doing wrong? Here's the code:
if(i.stadt=="T") { $("#stadtInput").removeClass("ui-input-text input"); $("#stadtInput").removeClass("ui-input-search input"); $("#stadtInput").addClass( 'textboxRight' ); } else { $("#stadtInput").addClass( 'textboxWrong' ); }
If I inspect the elements with Webdev-Tools of browser I can see that the textboxRight
/textboxWrong
class is added, but no class is removed. I already tried to remove all classes by .removeClass()
, but that doesn't work either..
jQuery removeClass() MethodThe removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.
The syntax for Removing CSS classes to an element:removeClass(class_name);
I'm not sure if this will fit your specifications, but the jQuery attr() method will take two arguments and replace all of the classes with the class name your provide. This won't work if you have other classes that you want to keep on your element, but if not try:
$("#stadtInput").attr("class", "textboxRight");
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