javascript create checkbox with label code example

Example: javascript create checkbox with label

const inputActiveElement = document.createElement('input');
inputActiveElement.classList.add('mr-2');
inputActiveElement.id = `active_image`;
inputActiveElement.type = 'checkbox';
inputActiveElement.value = '123';
inputActiveElement.name = 'active_image[]';
inputActiveElement.setAttribute('checked','true');

const labelActiveElement = document.createElement('label');
labelActiveElement.setAttribute('for',`active_image`);
labelActiveElement.innerText = 'Active';

const divInnerElement = document.createElement('div');
divInnerElement.classList.add('form-inline');
divInnerElement.classList.add('mt-2');
divInnerElement.append(inputActiveElement, labelActiveElement);

Tags:

Misc Example