I have this html and I am trying to get the input value of a certain checkbox
var option = document.createElement('li');
var checkbox = document.createElement('input');
var label = document.createElement('label')
var optionId = "option" "" questionNumber subquestionNumber;
var checkBoxId = "checkbox" "" questionNumber subquestionNumber;
var labelId = "label" "" questionNumber subquestionNumber;
var labelname = "name" "" questionNumber subquestionNumber;
checkbox.type = "checkbox";
label.id = labelId;
label.name= labelname;
option.id = optionId;
option.className = "optionName";
checkbox.id = checkBoxId;
checkBoxId.htmlFor = option;
label.appendChild(document.createTextNode('Option ' subquestionNumber));
option.appendChild(label); //add `label` to `li`
option.appendChild(checkbox); //add `checkbox` to `li`
q.appendChild(option);
var checkboxes = document.querySelector(labelId);
document.getElementById("testingBox6").innerHTML = checkboxes;
I want to save the input value to the variable checkboxes
Thank you for your time
CodePudding user response:
With Jquery
, it can be simple. But, you can try this without Jquery
document.getElementById(checkboxId).checked;
CodePudding user response:
you can get true/false with checked
for example
var checkbox = document.createElement('input');
checkbox.setAttribute("type", "checkbox");
checkbox.addEventListener("change", ()=> {
console.log(checkbox.checked); // it gives true or false when you clicked
})
CodePudding user response:
Or even you can use
document.querySelector('#checkBoxId').checked;