jQuery mobile - change ui-page background color dynamically

css, javascript, jquery, jquery-mobile

Solution

When the `pagecreate` event fires, the pseudo-page has not yet been given the `.ui-page` class, so selecting by that class won't help you. I suggest using `this` to select the current pseudo-page instead:

$(document).delegate("#pageDetail", "pagecreate", function () {
    $(this).css('background', '#ECF2FE');//`this` refers to `#pageDetail`
});

Also notice I updated the `background` property rather than `background-color`. This is because jQuery Mobile uses gradient backgrounds which are specified under the `background-image` property and you can't overwrite `background-image` with `background-color`.

Here is a demo: http://jsfiddle.net/WSzq3/ (when you click between pages the background color changes on each `pagebeforeshow` event)

Problem

I have a multi-page template setup. My home screen background color is different from the other pages, but I'm finding that some of the content I'm adding into the pages isn't long enough to reach the footer and I'm seeing the default background color set for the home screen between the background and the footer. Is there a way to change the ui-page background color for a page before switching to it, so it fills up the gap properly? I'd considered doing it by delegate for the page: ``` $(document).delegate("#pageDetail", "pagecreate", function () { $('.ui-page').css('background-color', '#ECF2FE'); }); ``` but that breaks when you hit the back button as it leaves the change in place. Is there an event I can use to make this change that will fire so I can set the correct background color for each page - or is there a simpler method I'm missing? Thanks

Original source