Dynamically create a checkbox with JavaScript/jQuery
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
document.getElementById('submit').onclick = function() { var checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.id = 'car'; checkbox.name = 'interest'; checkbox.value = 'car'; var label = document.createElement('label') label.htmlFor = 'car'; label.appendChild(document.createTextNode('Car')); var br = document.createElement('br'); var container = document.getElementById('container'); container.appendChild(checkbox); container.appendChild(label); container.appendChild(br); } |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create Checkbox</button> </div> </body> </html> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
p, label { font: 15px 'Exo 2', sans-serif; } input { margin: 6px; } button { margin-top: 10px; padding: 2 2; font: 15px 'Exo 2', sans-serif; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$(document).ready(function() { $('#submit').click(function() { $('#container').append( $(document.createElement('input')).prop({ id: 'myCheckBox', name: 'interest', value: 'car', type: 'checkbox' }) ).append( $(document.createElement('label')).prop({ for: 'myCheckBox' }).html('Car') ).append(document.createElement('br')); }) }); |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create Checkbox</button> </div> </body> </html> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
p, label { font: 15px 'Exo 2', sans-serif; } input { margin: 6px; } button { margin-top: 10px; padding: 2 2; font: 15px 'Exo 2', sans-serif; } |
Alternatively, here’s a shorter version:
JS
1 2 3 4 5 6 7 8 |
$(document).ready(function() { $('#submit').click(function() { $('#container') .append('<input type="checkbox" id="car" name="interest" value="car">') .append('<label for="car">Car</label></div>') .append(`<br>`); }) }); |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create Checkbox</button> </div> </body> </html> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
p, label { font: 15px 'Exo 2', sans-serif; } input { margin: 6px; } button { margin-top: 10px; padding: 2 2; font: 15px 'Exo 2', sans-serif; } |
To create multiple checkboxes dynamically, you can do something like:
JS
1 2 3 4 5 6 7 8 9 10 11 |
$(document).ready(function() { $('#submit').click(function() { var list = ['Car', 'Bike', 'Scooter']; for (var value of list) { $('#container') .append(`<input type="checkbox" id="${value}" name="interest" value="${value}">`) .append(`<label for="${value}">${value}</label></div>`) .append(`<br>`); } }) }); |
HTML
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create Checkbox</button> </div> </body> </html> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
p, label { font: 15px 'Exo 2', sans-serif; } input { margin: 6px; } button { margin-top: 10px; padding: 2 2; font: 15px 'Exo 2', sans-serif; } |
That’s all about dynamically creating a checkbox in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)