I want to create checkboxes dynamically. I am doing that, however I am failing with setting the name of the label. I tried setting the inner html to a value, though it didn't work. What is the correct way to do it ? source:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<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/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<div data-role="page" >
<fieldset data-role="controlgroup" id="exampleGroup">
</fieldset>
</div><!-- /page -->
</body>
<script>
var someId = "Something";
for (var i = 0; i < 5; i++) {
var checkBox = $('<input>', {
type: "checkbox",
class: "custom",
id: someId + i.toString()
});
var checkBoxLabel = $('<label>', {
for : someId + i.toString(),
});
checkBoxLabel.innerHTML = "Hello world!"; // didn't work
checkBox.appendTo("#exampleGroup");
checkBoxLabel.appendTo("#exampleGroup");
}
</script>
</html>
.innerHTMLis a property of DOM elements, not jQuery elements. If you use jQuery then use jQuery methods, don't mix & match with vanilla JS. See api.jquery.com/html or api.jquery.com/text.