Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .removeClass() not working

Tags:

jquery

css

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..

like image 892
Tobias Kuess Avatar asked Feb 24 '14 21:02

Tobias Kuess


People also ask

How to removeClass using jQuery?

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.

How do you remove a class in CSS?

The syntax for Removing CSS classes to an element:removeClass(class_name);


1 Answers

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"); 
like image 152
Alec Moore Avatar answered Oct 27 '22 00:10

Alec Moore