<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
HTML DOM Input Checkbox object
</
title
>
<
style
>
body {
text-align: center;
}
h1 {
color: green;
}
</
style
>
</
head
>
<
body
>
<
h1
>GeeksforGeeks</
h1
>
<
h2
>Creating an Input Checkbox Object</
h2
>
<
p
>Click the button to create a checkbox.</
p
>
<
button
onclick
=
"geek()"
>Click me!</
button
>
<
br
>
<
div
id
=
"myDiv"
></
div
>
<
script
>
function geek() {
let myDiv = document.getElementById("myDiv");
// creating checkbox element
let checkbox = document.createElement('input');
// Assigning the attributes to created checkbox
checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
checkbox.id = "id";
// creating label for checkbox
let label = document.createElement('label');
// assigning attributes for the created label tag
label.htmlFor = "id";
// appending the created text to
// the created label tag
label.appendChild(document.createTextNode('This is the label for checkbox.'));
// appending the checkbox and label to div
myDiv.appendChild(checkbox);
myDiv.appendChild(label);
}
</
script
>
</
body
>
</
html
>