So, I was coding on codepen.io and I was typing down some checkbox buttons.
I need to know what value checkbox buttons give off when checked.
for example an input with the text ‘INPUT’ inside it has a value of ‘INPUT’, but does a checked checkbox button have a value of ‘checked’.
HELP PLEASE.
For input of type checkbox the default value returned from the value
property is “on”, unless you specify a value
attribute on the element and give it a value (value="some-string"
). It is used when submitting the form usually together with the name attribute (name=value).
What you are probably looking for is the checked property, it gives you back a Boolean of true or false.
1 Like
So, do I just give it a value and when I check it it will be ‘on’??
Is your goal to test if a checkbox is checked? If so you want the checked property (Element.checked)
<input id="checkBox" type="checkbox">
const checkBox = document.querySelector('#checkBox');
console.log(checkBox.checked);
The value returned by the value property (Element.value) is “on”. Unless you give the element a value attribute and set it to something.