How do i create dynamic Checkbox in jQuery?

40,543
$('#containerId').append('<input type="checkbox" name="myCheckbox" />');

where containerId is the id of the DOM element you want to add the checkbox to.

Or alternative syntax...

$('#containerId')
    .append(
       $(document.createElement('input')).attr({
           id:    'myCheckbox',
           name:  'myCheckbox',
           value: 'myValue',
           type:  'checkbox'
       })
    );
Share:
40,543
Admin
Author by

Admin

Updated on August 01, 2020

Comments

  • Admin
    Admin almost 4 years

    I need to create dynamic checkbox using jQuery. How do i do that? Any code snippet will be helpful.

  • kamilah carlisle
    kamilah carlisle almost 4 years
    I really like the literal option for labels but how can I use variables to fill out the tag? For example, var name = name; $('#containerId').append('<label>name</label>');