HTML5 Zone is brought to you in partnership with:

Raymond Camden is a developer evangelist for Adobe. His work focuses on web standards, mobile development and Cold Fusion. He's a published author and presents at conferences and user groups on a variety of topics. He is the happily married proud father of three kids and is somewhat of a Star Wars nut. Raymond can be reached via his blog at www.raymondcamden.com or via email at raymondcamden@gmail.com Raymond is a DZone MVB and is not an employee of DZone and has posted 134 posts at DZone. You can read more from them at their website. View Full User Profile

Dynamically Changing jQuery Mobile Buttons

07.14.2012
| 2750 views |
  • submit to reddit

Filed in the "In case you need to know" department, jQuery Mobile provides a basic API that allows you to modify buttons. This is more useful for creating new buttons, but also has uses for existing buttons. Specifically, I was looking for a way to swap out the theme of a button in a form based on what had been entered. Here is a simple example of this API in action:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Single page template</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>

<body>

<div data-role="page">

<div data-role="header">
<h1>Single page</h1>
</div>

<div data-role="content">
<a href="" data-role="button" id="testButton">Button Me</a>	
<button id="realButton">Real Button</button>	
</div>

<div data-role="footer">
<h4>Footer content</h4>
</div>

</div>

<script>
var themes = ["","a","b","c","d","e"];
var currTheme = 0;

$("#testButton,#realButton").on("click", function() {
var tb = $("#testButton");
var rb = $("#realButton");

currTheme++;
if(currTheme == themes.length) currTheme=0;
newTheme = themes[currTheme];

console.log("Setting theme to "+newTheme);

//Do the test button
$(tb).buttonMarkup({theme:newTheme})

//Do the real button
$(rb).buttonMarkup({theme:newTheme})

});
</script>

</body>
</html>

Most of the template is boilerplate jQuery Mobile code. Note though the two buttons in the content section. Both are set to have no theme. Now - look at the JavaScript code at the bottom. I've got a basic click handler (should be touch I suppose) that will iterate over all possible theme values, including no theme specified. The API allows you to tweak pretty much everything, but I'm not sure what else you would change on the fly. Icon perhaps. Anyway, on the off chance someone needs this, here is a demo of the code above.

 

Published at DZone with permission of Raymond Camden, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)