how to create checkbox dynamically using jquery

bruce
bruce
Member
40 Points
10 Posts

Hi,

how to create checkbox dynamically using jquery?

Views: 8840
Total Answered: 1
Total Marked As Answer: 1
Posted On: 09-Aug-2015 23:43

Share:   fb twitter linkedin
Answers
Brian
Brian
Moderator
2232 Points
14 Posts
         

Hi bruce,

 A checkbox is an input element with a type attributes of "checkbox". So you can do as following example do.

<div id="cblist"> 
<input type="checkbox" value="first checkbox" id="cb1" /> 
<label for="cb1">first checkbox</label>
</div>
<input type="text" id="txtName" />
<input type="button" value="Add Check box" id="btnSave" />
<script type="text/javascript">
$(document).ready(
function () {
$('#btnSave').click(function () {
addCheckbox($('#txtName').val());
});
});
 
function addCheckbox(name) { 
var container = $('#cblist'); 
var inputs = container.find('input'); 
var id = inputs.length + 1;
$('<input />', { type: 'checkbox', id: 'cb' + id, value: name }).appendTo(container);
$('<label />', { 'for': 'cb' + id, text: name }).appendTo(container);
}
</script> 

 In Above example we add new check box with the given value.

 

Posted On: 10-Aug-2015 00:02
 Log In to Chat