I can't seem to find an example anywhere... what's the correct way of doing a HTML5 checkbox?
The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!
The Input Checkbox name property in HTML DOM is used to set or return the value of name attribute of a input checkbox field. The name attribute is required for each input field. If the name attribute is not specified in an input field then the data of that field would not be sent at all.
If you wanted to submit a default value for the checkbox when it is unchecked, you could include an <input type="hidden"> inside the form with the same name and value , generated by JavaScript perhaps.
As far as I know and the docs state, nothing fundamental has changed. The basic markup is
<input name="your_name" value="your_value" type="checkbox">
What is new is some interesting properties.
form
- a reference to the form the control is associated with (nice!)autofocus
- to be focused as soon as the document is loaded (nice!)required
- require that it be checked (super nice! Although it isn't supported by Internet Explorer or Safari (yet).)A more complete example - and avoiding the long stream of posts to How to check whether a checkbox is checked in jQuery?.
HTML
<input id="your_id" name="your_name" value="your_value" type="checkbox">
Optionally add the 'checked' attribute to default to checked on load.
<input id="your_id" name="your_name" value="your_value" type="checkbox" checked>
JavaScript
$('#your_id').is(':checked') // Returns a Boolean TRUE if checked
e.g.
if ($('#your_id').is(':checked')) { // Checkbox was checked }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With