Dynamically Create Button Style
by bizamajig
HTML
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css">
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Add Elements</title>
</head>
<body>
<div data-role="page" id="master">
<div data-role="header">
<h1>Add Elements</h1>
</div>
<div data-role="content" id="content">
<p>
Adds Button with predefined HTML fragment.
</p>
<a id="addbutton1" data-role="button">Add Buttton</a>
<div id="addbutton1_here">
</div>
<p>
Adds Button with button() method.
</p>
<a id="addbutton2" data-role="button">Add Buttton</a>
<div id="addbutton2_here">
</div>
</div>
</div>
</body>
</html>
JavaScript
function getTime() {
var date = new Date();
return pad(date.getHours(), 2) + ':' + pad(date.getMinutes(), 2) + ':' + pad(date.getSeconds(), 2);
}
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
function resetButton(button) {
setTimeout(function() {
$(button).removeClass('ui-btn-active');
}, 500);
}
$(document).ready(function () {
$("a[data-role='button']").live('click', function() {
resetButton($(this));
});
$('#addbutton1').bind('click', function() {
var s = '<a class="ui-btn ui-btn-corner-all ui-shadow ui-btn-up-c" data-role="button" data-theme="c">' +
'<span class="ui-btn-inner ui-btn-corner-all">' +
'<span class="ui-btn-text">' + getTime() + '</span>' +
'</span>' +
'</a>';
$("#addbutton1_here").append(s);
});
$('#addbutton2').bind('click', function() {
$("#addbutton2_here").append('<a data-role="button">' + getTime() + '</a>');
$("#addbutton2_here").find("a[data-role='button']").button();
});
});