Create checkbox list and bind action via Javascript

Description

The following code shows how to create checkbox list and bind action via Javascript.

Example


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type="text/javascript"
  src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.js"></script>
<link rel="stylesheet" type="text/css"
  href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.css">
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){<!--from w w w .  ja  v a  2  s. c o  m-->
    var modules = ['module1', 'module2', 'module3'];
    var html='';
    for(var i=0; i<modules.length; i++) {
        var moduleName=modules[i];
        html+="<input type='checkbox' name='modules' id='"+moduleName+"' class='module' />";
        html+="<label for='"+moduleName+"'>"+moduleName+"</label>";
    }
    $("#moduleListTitle").html(html);
    $('#moduleListTitle').parent().trigger('create');
    $('input:checkbox.module').on('change', function(){
        console.log(this.tagName);
        console.log(this.id);
        console.log("-----");
    });
});//]]>  
</script>
</head>
<body>
  <div data-role="fieldcontain" id="moduleContainer">
    <fieldset data-role="controlgroup" id="moduleListTitle"></fieldset>
  </div>
</body>
</html>

Click to view the demo





















Home »
  jQuery Mobile »
    Example »




Button
Button Event
Button Icon
Button Text
Checkbox
Collapsible
Date
Dialog
Event
Footer
Form
Header
Icon
Input
Layout
Listview
Listview Divider
Listview Item
Loader
mobile_settings
Navbar
Page
Page Event
Popup
Radio
Select
Slider
Switch
Textarea
Theme