Photo by Jackson Sophat on Unsplash
Input Elements
In this article we'll talk about input elements in HTML.
Table of contents
HTML (HyperText Markup Language) is used to create the structure and content of web pages. Input elements are an important part of HTML as they allow users to enter data into web forms and they make a webpage more interactive. In this article, we will explain some of the basic input elements in HTML and how they work. HTML provides a range of input elements, including text input, password input, checkboxes, and radio buttons. By understanding these basic input elements, you can start creating your own web forms and collecting data from your website visitors.
Text Input
Text input is one of the most common input elements in HTML. It allows users to enter a single line of text into a form field. To create a text input field, use the element with the type attribute set to "text". For example:
<label for="name">Name:</label>
<input type="text" id="name" name="name">
In the example above, the label element provides a label for the input field, and the input element creates the actual text input field.
Password Input
Password input is similar to text input, but it masks the user's input with asterisks or bullets to keep it secure. To create a password input field, use the element with the type attribute set to "password". For example:
<label for="password">Password:</label>
<input type="password" id="password" name="password">
Checkbox
Checkboxes allow users to select one or more options from a list. To create a checkbox input field, use the element with the type attribute set to "checkbox". For example:
<label for="option1">Option 1</label>
<input type="checkbox" id="option1" name="options[]" value="1">
<label for="option2">Option 2</label>
<input type="checkbox" id="option2" name="options[]" value="2">
In the example above, the name attribute is set to "options[]" to allow multiple options to be selected and submitted as an array.
Radio Button
Radio buttons are similar to checkboxes, but only one option can be selected from a list. To create a radio button input field, use the element with the type attribute set to "radio". For example:
<label for="option1">Option 1</label>
<input type="radio" id="option1" name="option" value="1">
<label for="option2">Option 2</label>
<input type="radio" id="option2" name="option" value="2">
In the example above, the name attribute is set to "option" to ensure only one option can be selected.