This post will discuss how to create a dynamic checkbox using JavaScript and jQuery.

To create a checkbox dynamically, you would need to create the checkbox, its label, and optionally a <br> tag.

1. Using JavaScript

In pure JavaScript, you can use the document.createElement() method to programmatically create a checkbox element.

The following example creates a new checkbox with necessary attributes and appends it to a container using the Node.appendChild() method.

JS


HTML


CSS



Edit in JSFiddle

2. Using jQuery

With jQuery, you can set checkbox attributes using the .prop() method and .append() method to append the checkbox at the end of a container. This is demonstrated below:

JS


HTML


CSS



Edit in JSFiddle

 
Alternatively, here’s a shorter version:

JS


HTML


CSS



Edit in JSFiddle

 
To create multiple checkboxes dynamically, you can do something like:

JS


HTML


CSS



Edit in JSFiddle

That’s all about dynamically creating a checkbox in JavaScript and jQuery.