DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Dynamically Changing jQuery Mobile Buttons

Dynamically Changing jQuery Mobile Buttons

Raymond Camden user avatar by
Raymond Camden
·
Jul. 14, 12 · Interview
Like (0)
Save
Tweet
Share
5.48K Views

Join the DZone community and get the full member experience.

Join For Free

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.

JQuery Mobile

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Quick Pattern-Matching Queries in PostgreSQL and YugabyteDB
  • Bye-Bye, Regular Dev [Comic]
  • 5 Factors When Selecting a Database
  • Mind Map Reuse in Software Groups

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: