Dynamically change jQuery Mobile colour swatch.
There’s no easy way to be able to switch the jQuery Mobile colour swatch dynamically for the whole page. A quick google search suggests this stackoverflow question where the accepted answer suggests removing and adding relevant classes for the required colour swatch.
For instance :
There are a bunch of other elements that you’ll need to update as mentioned in the answer. It seems to work! But I am not too sure if this approach will cover all jQM supported elements.
I found another (hacky?) way which works and should cover each and every jQM supported element.
The straightforward way to apply a particular colour swatch is to use the data-theme attribute. So, to apply the colour swatch ‘a’ to a page :
Now, to be able to switch themes, another approach would be to have two css stylesheets for the same colour swatch ‘a’ and we could just switch the stylesheet using js whenever we needed to change the theme. This’ll ensure that we do not need to update the class values for each element as mentioned in the stackoverflow answer since all properties for the theme ‘a’ are already applied to elements.
To generate these stylesheets we use the themeroller. In general, the themeroller is used to generate a stylesheet for different swatches (a, b, ..z) which could then be applied by setting the data-theme attribute. But in our case, we’ll use it generate two different stylesheets by the name ‘a’.
As an example, we’ll use the default stylesheet provided by jQM to generate a light and a dark theme. Here’s a short video tutorial about downloading the two stylesheets :
Now, let’s assume that we want to start with the light theme. So, we include the following in the head of the html page.
with the data-theme attribute set to ‘a’ for the page element.
Now, whenever you need to update the colour swatch :
Updating based on radio button selection :
Hope this helps!