The checkbox is a type of input element available in HTML that allows users to check it or leave it unchecked and allows users to select or deselect the option displayed on the web page. The checkbox enables an application to display input for selection in the form of a box and provides the user access to select or deselect the displayed option. HTML checkbox can be used for user selection in the form of a yes/no statement or agreement kind of statement; depending upon this value, different functionality can be achieved.
Syntax:
Just like for other input type parameters, we will change the type of input to a ‘checkbox’ in the case of a checkbox.
<input type="checkbox">
Just like other types of input, we can add additional parameters in the syntax.
Function of HTML checkbox Tag
- The HTML checkbox Tag assigns a value as true or false to the input element as the parameter ‘checked.’
- When we click on the checkbox, it modifies the value of this element as true or false, which can be used further for checking.
Examples of HTML checkbox Tag
Given below are the examples of HTML checkbox Tag:
Example #1 – Create a Simple Checkbox.
Code:
<title> Checkbox in HTML </title> <style> .results { border : green 1px solid; background-color : aliceblue; text-align : left; padding-left : 20px; height : 150px; width : 95%; } .resultText { font-size : 20px; font-style : normal; color : blue; } </style> <div class="results"> <h2 id="Check-Box-Example"> Check Box Example: </h2> <!-- Declare input box with type as checkbox, we have also assigned name to this element--> Checkbox 1 <input type="checkbox" name="checkbox1"> Checkbox 2 <input type="checkbox" name="checkbox2"> <p id="result"> </p> </div>
Output:
Here, we have declared two input elements as Checkbox 1 and Checkbox 2 on the web page. We have not taken any action on clicking on a checkbox.
Example #2 – Multiple select elements.
Code:
<title> Checkbox in HTML </title> <style> .results { border : green 1px solid; background-color : aliceblue; text-align : left; padding-left : 20px; height : 200px; width : 95%; } .resultText { font-size : 20px; font-style : normal; color : blue; } </style> <div class="results"> <h2 id="Check-Box-Example"> Check Box Example: </h2> <!-- Declare two input boxes with type as checkbox --> <h4 id="Choose-languages"> Choose languages </h4> <div> <input type="checkbox" name="English"> <label for="English"> English </label> </div> <div> <input type="checkbox" name="Hindi"> <label for="Hindi"> Hindi </label> </div> <div> <input type="checkbox" name="German"> <label for="German"> German </label> </div> <div> <input type="checkbox" name="French"> <label for="French"> French </label> </div> <p id="result"> </p> </div>
Output:
This example shows how we can create multiple checkbox input elements simultaneously. Here, we have created a total of four checkbox elements for choosing the languages. Note that we can select more than one checkbox at a time; this contrasts with radio button input, where only one option can be chosen from all displayed element options.
Example #3 – Checked Attribute.
As observed from the examples until that, the checkbox is unchecked on a web page loading. If we want to display a checkbox with checked as default, we can use the “checked” attribute with the checkbox element.
Code:
<title> Checkbox in HTML </title> <style> .results { border : green 1px solid; background-color : aliceblue; text-align : left; padding-left : 20px; height : 200px; width : 95%; } .resultText { font-size : 20px; font-style : normal; color : blue; } </style> <div class="results"> <h2 id="Check-Box-Example"> Check Box Example: </h2> <!-- Declare two input boxes with type as checkbox --> <h4 id="Choose-languages"> Choose languages </h4> <div> <input type="checkbox" name="English" checked> <label for="English"> English </label> </div> <div> <input type="checkbox" name="Hindi" checked> <label for="Hindi"> Hindi </label> </div> <div> <input type="checkbox" name="German"> <label for="German"> German </label> </div> <div> <input type="checkbox" name="French"> <label for="French"> French </label> </div> <p id="result"> </p> </div>
Output:
Here, out of four checkbox elements, we have made two checkboxes to load as checked by default. Note the first two languages are displayed as checked by default.
Example #4 – Checkbox in HTML Form.
Another way of using the checkbox. It will be added in HTML form, and we will see how to identify whether a checkbox is checked.
Code:
<title> Checkbox in HTML </title> <style> .results { border : green 1px solid; background-color : aliceblue; text-align : left; padding-left : 20px; height : 200px; width : 95%; } .resultText { font-size : 20px; font-style : normal; color : blue; } </style> <div class="results"> <h2 id="Check-Box-Example"> Check Box Example: </h2> <form> <div> Checkbox 1 <input type="checkbox" name="checkbox1" id="selected" value="Yes"> </div> <br> <div> Checkbox 2 <input type="checkbox" name="checkbox2" id="selected" value="Yes"> </div> <div> <button type="submit"> Submit </button> </div> </form> <p id="result"> </p> </div>
Output:
Here, we have included checkbox elements within a form element. This is useful for processing the value of the checkbox on the server. The submission will include the checkbox values when sending the form to the server. The URL value is generated by combining the checkbox name and the value attribute in the checkbox element.
For example, in our case, when both checkboxes are sent as checked, the URL will contain “checkbox1=Yes&checkbox2=Yes” in it.
Conclusion
An input element called a checkbox allows users to choose or unchoose an option presented to them in HTML. In this article, we have seen multiple use cases of the same.
The above is the detailed content of HTML checkbox Tag. For more information, please follow other related articles on the PHP Chinese website!

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
