How To Get The Values Of Checkbox Instead Of True
I wanted to print the values of selected checkboxes instead of 'Custom Category'. I have tried to get value, attribute but not getting the values. I wanted to print the values of t
Solution 1:
According to specs, an input type="checkbox" has two values (true/false). It even warns about preset "values". You've two ways to get what you ask for:
- Use element.id.toUpperCase()
- Use Data attributes
Solution 2:
will this small example help?
consttest = () => {
const selected = [...document.querySelectorAll('input[type="checkbox"]:checked')].map(cb => {
return cb.value;
});
console.log(selected);
}
<inputclass="form-check-input"type="checkbox"id="dl1"required #dl="ngModel"value="DL1"ngModelname="dl"><labelclass="form-check-label"for="">DL 1</label><inputclass="form-check-input"type="checkbox"id="dl2"required #dl="ngModel"value="DL2"ngModelname="dl"><labelclass="form-check-label"for="">DL 2</label><buttononclick="test()">Test</button>
Post a Comment for "How To Get The Values Of Checkbox Instead Of True"