How to change styleName dynamically

I am trying to change the styleName of a Label dynamically (e.g when i press a Button) by using the following code:


 label1.removeStyleName("oldStyleName");
 label1.addStyleName("newStyleName");
 label1.markAsDirty();

The problem is that no change is happening on the browser, i.e the new style name is not applied although it is declared in the css file.

Hi,

First of all, there should be no need to call markAsDirty().

Second, have you verified, by using Firebug or Developer tools, that the class name is really not added to the DOM element? If not, then your style is probably overridden or then your css selector does not match yout style name OR you need to clear browser cache before the css changes can be seen.

Ok, fortunately there was a typo on the style name in the CSS file…
Seems to work now.