1

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

3 Answers 3

1

With Jquery, it can be simple. But, you can try this without Jquery

document.getElementById(checkboxId).checked;

Sign up to request clarification or add additional context in comments.

3 Comments

I get this error when I try Hai's suggestion. checkboxId is not defined
Nevermid, thank you it worked. The checkBox was written like this. Thank you
checkboxId follow your code on above question. I follow that. You can replace a particaly ID document.getElementById('your-checkbox-id').checked;
1

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
})

Comments

1

Or even you can use

document.querySelector('#checkBoxId').checked;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.