GOAL:
Dynamically add form elements—in this case a number of checkboxes—to a form.
PROBLEM:
My code produces only 1 checkbox.
ASSUMPTION:
- Scope issue.
- Once an html element is created and then attached to another element in the DOM or vDOM using jQuery, it cannot be reused again and I need to build another.
CODE:
$pform = $('<form/>'); // Create a form element.
$pform_checkbox = $('<input/>').attr('type','checkbox'); // Create checkbox element.
$('body').append($pform); // Add form element within the body element.
//Goal is to add 5 checkboxes to the form.
//The result is only one checkbox is added. Why?
for (var x = 0; x<5; x++){
//creating the checkbox element here will give me the proper result.
//$pform_checkbox = $('<input/>').attr('type','checkbox')
$pform.append($pform_checkbox);
}
JSFIDDLE: