Life saving jQuery Mobile Code Snippets

Life saving jQuery Mobile Code Snippets

Hello everyone, welcome to my today’s post. Today I’m going to present a new (may be not for all) topics and it’s jQuery Mobile. We all knows very well , World getting smaller day by day because technologies are growing rapidly! Now Computer, Laptop and mobile become a part of our life. We can’t spend a single day without them. Personally, I love to spent maximum time with my Laptop. It’s a good friend of mine!

Today, I like to share some of my favorite scripts that I had frequently used in jQuery mobile framework. First of all I like to say, you need to learn about jQuery before starting jQuery mobile. Because all things in jQuery Mobile based on jQuery core API. So, lets start-

1. Disable truncation for list items and buttons:

If your list item or button has a long text, it will be truncated automatically by jQuery Mobile. To disable this truncation, add “white-space: normal;” to the CSS in question. For example, to disable truncation for buttons:

.ui-btn-text {
white-space: normal;

To disable truncation for list descriptions:

.ui-li-desc {
white-space: normal;

To enable truncation, set it to “white-space: nowrap;“.

2. Display a random background image on page load:

jQuery Mobile has a number of page initialization events that you can use to trigger certain methods on page load. The following CSS + Javascript can be used to display a random background image every time a page is loaded.


.my-page  { background: transparent url(../images/bg.jpg) 0 0 no-repeat; }

.my-page.bg1 { background: transparent url(../images/bg-1.jpg) 0 0 no-repeat; }

.my-page.bg2 { background: transparent url(../images/bg-2.jpg) 0 0 no-repeat; }

.my-page.bg3 { background: transparent url(../images/bg-3.jpg) 0 0 no-repeat; }


$('.my-page').live("pagecreate", function() {
    var randombg = Math.floor(Math.random()*4); // 0 to 3
    $('.my-page').removeClass().addClass('bg' + randombg);

3. Disable a button action

To disable a button action (for eg: from opening a page), add the following Javascript.


And to re-enable it:


4. Disable loading pop-up message

I find the loading pop-up message a bit annoying because it gets triggered everytime you load a different page. To disable this add the following line of code into your JS file.


By default, it is enabled like so:


5. Create a custom theme

jQuery Mobile framework comes with 5 themes – Theme A, Theme B, Theme C, Theme D and Theme E. But you can easily create a new theme for your web app.

The steps to create a new theme:

1. Copy CSS for any theme from jQuery Mobile CSS file and paste it into your own CSS file.

2. Give your theme a name and rename the CSS appropriately. Your theme name can be any alphabet character
(a to z). So for example, if you copied Theme C, and you want to call your theme Theme Z, rename .ui-btn-up-c to .ui- btn-up-z, .ui-body-c to .ui-body-z and so on.

3. Change colors and styles of your custom theme

4. To apply your custom theme z to any element, just set the data-theme attribute to z. For example:

<div data-role="page" data-theme="z"></div>

6. Use a custom font

There are a few font-replacement methods available such as cufon, sIFR, FLIR, @font-face and Google Fonts API. When building a web app using jQuery Mobile, I found that @font-face method is the easiest method to work with and the performance is quite satisfactory. If you are interested in @font-face, here is a helpful tutorial with a demo on how to work with @font-face method.

7. Create an image-only button with no text

Sometimes, you may not want to have any text for your button but still use the rest of the features that comes with a button element. This is usually the case with a home button or an info button. To hide any text associated with the button, set data-iconpos attribute to “notext”. For example:

<div data-icon=”grid” data-iconpos=”notext”></div>

8. Open a link without using AJAX with page transitions

To open a link without using AJAX with page transitions (ie: reloading the full page the old-school way), set rel attribute to “external”.

<div class="ui-btn-right" data-icon="grid" rel="external"></div>

9. Remove an arrow from a list item

By default, jQuery Mobile framework adds an arrow next to every list item. To disable this, set data-icon attribute to “false” on the list item that you’d like the arrow to be removed.

<button data-icon="false"></button>

10. Set background color of a page

This may sound simple but it took me a few minutes to figure out how to apply a background color to a page without having it overwritten by jQuery Mobile CSS. Normally, you’d set a background color to body element but if you are using jQuery Mobile framework, you need to set it to ui-page class.

.ui-page {
background: #eee;

That’s all for today. Hope I’ll get back soon with new tutorials.

Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!

Back To Top