You can target specific widgets and update their themes by changing the theme-specific classes:
//set a theme letter to change to
var theme = 'a';
//update the button widgets on the current page
$.mobile.activePage.find('.ui-btn').not('.ui-li-divider')
.removeClass('ui-btn-up-a ui-btn-up-b ui-btn-up-c ui-btn-up-d ui-btn-up-e ui-btn-hover-a ui-btn-hover-b ui-btn-hover-c ui-btn-hover-d ui-btn-hover-e')
.addClass('ui-btn-up-' + theme)
.attr('data-theme', theme);
//update the list-divider elements
$.mobile.activePage.find('.ui-li-divider').each(function (index, obj) {
if ($(this).parent().attr('data-divider-theme') == 'undefined') {
$(this).removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e')
.addClass('ui-bar-b')
.attr('data-theme', 'b');
}
})
//update the header, footer, and body
$.mobile.activePage.find('.ui-header, .ui-footer')
.removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e')
.addClass('ui-bar-' + theme)
.attr('data-theme', theme);
$.mobile.activePage.removeClass('ui-body-a ui-body-b ui-body-c ui-body-d ui-body-e')
.addClass('ui-body-' + theme)
.attr('data-theme', theme);
Here is a link to a demo of this code: http://jsfiddle.net/VNXb2/7/
Here is another answer I posted about this: How to change theme dynamically in jquery mobile?
The above code is for switching the theme after initialization (that's why there is all the class-switching), if you want to switch the theme before the jQuery Mobile framework has initialized anything, you can use an attribute selector to alter all the elements in the DOM:
//this will update any element with a `data-theme` attribute set so it's set to `a`
$('[data-theme]').attr('data-theme', 'a');
If you want to target different types of widgets to be different themes you can just make the selector a bit more specific:
//give all form inputs the `a` theme
$('input').attr('data-theme', 'a');
//give all the header elements the `a` theme
$('[data-role="header"]').attr('data-theme', 'a');
You could force a reload when the user chooses a swatch, then load the swatch as a GET variable, and read that variable when the page loads and before jQuery Mobile has a change to initialize anything:
<script src="jQuery-Core.js"></script>
<script>
//check if there are any GET variables
if (window.location.search != '') {
var swatch = '',
arr = window.location.search.replace('?').split('&');
//loop through the GET variable key/pairs
for (var i = 0; len = arr.length; i < len; i++) {
//split into key/pair
var pair = arr[i].split('=');
//check if the key is `swatch` and if so then set the `swatch` variable to its value
if (pair[0] === 'swatch') {
swatch = pair[1];
}
}
//the `swatch` variable now holds the GET variable `swatch` if it was set
if (swatch !== '') {
$('[data-theme]').attr('data-theme', swatch);
}
}
</script>
<script src="jQuery-Mobile.js"></script>
Notice the order of the <script>
tags.