The exact appearance depends upon the operating system configuration under which the browser is running. Generally this is a square but it may have rounded corners. A checkbox allows you to select single values for submission in a form or not.
Note : Radio buttons are similar to checkboxes, but with an important distinction — radio buttons are grouped into a set in which only one radio button can be selected at a time, whereas checkboxes allow you to turn single values on and off. Where multiple controls exist, radio buttons allow one to be selected out of them all, whereas checkboxes allow multiple values to be selected.
A DOMString representing the value of the checkbox. This is never seen on the client-side, but on the server this is the value given to the data submitted with the checkbox's name. Take the following example:.
Handling Checkboxes Onclick
In this example, we've got a name of subscribeand a value of newsletter. Note : If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state e. If you wanted to submit a default value for the checkbox when it is unchecked, you could include an elements of type hidden let web developers include data that cannot be seen or modified by users when a form is submitted.
For example, the ID of the content that is currently being ordered or edited, or a unique security token. Hidden inputs are completely invisible in the rendered page, and there is no way to make it visible in the page's content. In addition to the common attributes shared by all element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent.
A Boolean attribute indicating whether or not this checkbox is checked by default when the page loads. Note: Unlike other input controls, a checkboxes value is only included in the submitted data if the checkbox is currently checked.
If it is, then the value of the checkbox's value attribute is reported as the input's value. Use the autocomplete attribute to control this feature. If the indeterminate attribute is present on the element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent.
When you are handling a group of related checkboxes onclick, using getElementById may not be the most practical way of obtaining references to them. We use the following form to demonstrate an alternative approach:. To obtain references to the checkboxes in the form, we enclose the checkboxes in a container with an ID.
We get a reference to that container using its ID, then use getElementsByTagName to get references to the input elements inside it. Next we use a for loop to inspect the type property of each of the input elements in the node list returned by getElementsByTagName.
Our example form assigns an updateTotal function to the onclick property of each checkbox. As explained in Assigning Functions to Event Handler Propertiesa function assigned to an event handler property of an object has access to that object inside the function via the this keyword.
In the updateTotal function, a reference to the form containing the checkboxes is obtained through the this keyword, as is the value of the checkbox clicked. Since the values of form elements are always strings, the parseFloat function is used to convert the current value of the total text box to a floating point number.
Then the checkbox's checked property is examined. If the checkbox is checked, its value is added to the current value in the total text box. If not, the value is subtracted from the total.
Toppings: mushrooms onions black olives sausage pepperoni. We could access other properties of the checkbox the same way, as explained further in the Event Handler Properties Example. Other methods of Attaching Event Handlers could also be used. Like dyn-web site speed? Hosted by Digital Ocean!Additionally, you can use use the querySelector to check if the :checked selector does not return nulllike this:. If a checkbox does not have the value attribute, its default value is 'on' :. If you get the value attribute of a checkbox, you always get the 'on' string whether the checkbox is checked or not.
For example:. Assuming that you have three checkboxes and you want to get values of all the selected checkboxes:. To accomplish this, you need to add two more HTML attributes to each checkbox: name and value. All three checkboxes need to have the same name but distinct values, f or example:. Sometimes, you may want to check and uncheck all checkboxes on a form.
See the following example:. When you click the button with the id btnall the checkboxes will be checked if they are not, and unchecked if they are already checked. First, develop a check function that checks or unchecks all checkboxes based on an input argument:. When you click the button, you can call the check function to select all checkboxes. Next time, when you click the button, it should uncheck all the checkboxes.
If you click the button, the uncheck function is invoked to check all checkboxes. Then, it reassigns the uncheckAll function to the onclick event handler.
Next time, if you click the button, the uncheckAll function is invoked to uncheck all the checkboxes and reassign the checkAll function to the onclick event handler. Checking if a checkbox is checked To get the state of a checkbox, whether checked or unchecked, you follow these steps: First, select the checkbox using the selecting DOM methods such as getElementById or querySelector.
The value property only has meaning when submitting a form. If a checkbox is in checked state when the form is submitted, the name of the checkbox is sent along with the value of the value property if the checkbox is not checked, no information is sent.
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:. LOG IN. New User? Sign Up For Free! Forgot password? Example Change the value associated with the checkbox: document. Example Submitting a form - how to change the value associated with the checkbox: document.
Try to submit the form again. HOW TO. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
As you can see from the above examples, including checkboxes in your web pages is easy. The checked property allows you to set the checkbox initial value or return the value if the user has selected the checkbox or not.
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. If you want to have a variable always be the current state of the checkbox rather than having to keep checking its stateyou can modify the onchange event to set that variable.
This assumes the checkboxes to check have the class "messageCheckbox", otherwise you would have to do a check if the input is the checkbox type, etc. None of the above worked for me without throwing errors in the console when the box wasn't checked so I did something along these lines instead onclick and the checkbox function are only being used for demo purposes, in my use case it's part of a much bigger form submission function :.
Learn more. Get the value of checked checkbox? Ask Question. Asked 8 years, 2 months ago.
Active 9 months ago. Viewed k times. Richard Barker 1 1 gold badge 8 8 silver badges 27 27 bronze badges. Matthew 'mandatory' Bryant Matthew 'mandatory' Bryant 2, 5 5 gold badges 18 18 silver badges 28 28 bronze badges.
None of the above worked for me but simply use this: document. JanBorup JanBorup 4, 1 1 gold badge 25 25 silver badges 16 16 bronze badges. I concur. You confirmed what I thought. Where did you get that id? Can you please explain? The guy who asked the question used class in the input actually. This can't work with the example code provided by OP I'm curious why it got so many upvotes while not answering the question, although a quick fix might make it right.
May 15 '16 at Can you edit your proposed answer to expand on what this does and how it addresses the OP? PradeepKumarPrabaharan probably people like me not using id for multiple items wondering why. I am using this in my code.
This does not answer the question, but can be used to determine whether or not a checkbox is checked. Does not answer the question but is exactly what I was looking for.