jQM - Dynamic checkbox/radio DEMO
by Dragan Gaić
HTML
<link rel="stylesheet" href="http://jeromeetienne.github.com/jquery-mobile-960/css/jquery-mobile-fluid960.css">
<link rel="stylesheet" href="http://dev.jtsage.com/cdn/simpledialog/latest/jquery.mobile.simpledialog.min.css">
<script src="http://dev.jtsage.com/cdn/simpledialog/latest/jquery.mobile.simpledialog2.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>jQM Complex Demo</title>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
<a href="#second" class="ui-btn-right">Next</a>
</div>
<div data-role="content">
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
</body>
</html>
JavaScript
$(document).on('pagebeforeshow', '#index', function(){
// Add a new radio element
$('[data-role="content"]').append('<fieldset data-role="controlgroup"><legend>Choose a pet:</legend><input type="radio" name="radio-choice" id="radio-choice-1" value="choice-1" checked="checked" /><label for="radio-choice-1">Cat</label><input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2" /><label for="radio-choice-2">Dog</label></fieldset>');
$('[data-role="content"]').append('<fieldset data-role="controlgroup"><legend>Agree to the terms:</legend><input type="checkbox" name="checkbox-1" id="checkbox-1" class="custom" /><label for="checkbox-1">I agree</label></fieldset>');
// Enhance new radio element
$('[type="radio"]').checkboxradio();
$('[type="checkbox"]').checkboxradio();
// Select another radio element
$("input[type='radio']").eq(0).attr("checked",false).checkboxradio("refresh");
$("input[type='radio']").eq(1).attr("checked",true).checkboxradio("refresh");
});