Creating and working with forms in html. Creating a form in HTML Select, Option, Textarea, Label, Fieldset, Legend - drop-down lists, text areas and other web form elements
The HTML option tag is used to create a dropdown list that allows the user to select one option from a predefined set of values.
The text visible to the user may differ from the text specified in the value attribute. Here's how to create a dropdown list:
- The dropdown list is created using the tag
- Inside a tag
- In the tag
It is also possible to specify a CSS class instead of using an ID to style the dropdown list.
In the next section, I will show examples of using HTML dropdown list in JavaScript/JQuery. The examples will also show how to set styles
Example of creating a simple dropdown list
This example uses the HTML select option to create a dropdown list with three options to choose from:
The example above uses the following markup to create a dropdown list:
Using the value attribute
As mentioned earlier, the value of the value attribute may differ from the text displayed on the page. For example, you can display the names of countries or colors for users, and use shortcodes in the value attribute.
In the following example, we will create a dropdown list with a value attribute:
View online demo and code
For tag
Example of accessing the selected option in JavaScript
Now let's create an example of accessing the value of a selected option and performing some actions. We create the same list as in the example above with color options. Once selected, click the button to apply that color to the document:
View online demo and code
For option value HTML the following code is used:
The following line of code is used in JavaScript to access the value of an option's value attribute
var seltheme = document.getElementById(“selcolor”).value;
When the button is clicked, a JS function is called, which assigns the value selected in the drop-down list to a variable. This value is used to apply a color to the current document.
Accessing visible text in JQuery
This time I'll use JQuery to access the value of the selected option: both the text and the value . In this demo, I'll be accessing the visible text in an option tag in HTML:
View online demo and code
In the code for each option in the tag
Here's how to access this value in HTML select option selected JavaScript:
var selectedcolor = $("#jqueryselect option:selected").text();
You can also access the value using the JQuery $.val() method:
var selectedcolor = $("#jqueryselect").val();
Replace this line in the example above and the code will display the shortcode/color value in the value attribute rather than the visible text.
An example of getting a value in a PHP script
In this example of getting the value of the option selected from a dropdown list, the form is created using the tag
View online demo and code
The form method used in the example is POST, so you can get the form values using the PHP $_POST[“”] array. This is the form code used in the example:
And here's how the PHP script is used to get the HTML select option value:
". $_POST["selfphp"].""; } ?>
If the form indicates GET method, then use PHP array $_GET[“”].
Styling a Dropdown List with CSS
Now let's look at how to define dropdown list styles
View online demo and code
Together with linear-gradient, the box-shadow property is used here. The complete CSS code looks like this:
Selcls ( padding: 3px; border: solid 1px #517B97; outline: 0; background: -webkit-gradient(linear, left top, left 25, from(#FFFFFF), color-stop(4%, #CAD9E3), to (#FFFFFF)); background: -moz-linear-gradient(top, #FFFFFF, #CAD9E3 1px, #FFFFFF 25px); box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px; box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px; -webkit-box-shadow: rgba(0,0,0, 0.1) 0px 0px 8px;
Rounded corners using the border-radius property
For the HTML select option, we'll set the CSS3 border-radius property to make the corners rounded. The color scheme also changes. You can experiment with borders, width, margins and other properties as you wish:
View online demo and code
Using Multiple CSS Attributes and Styles
To allow users to select multiple options from a list, you need to use the multiple attribute. In the example above, only one option can be selected. When using multiple, you can select multiple options by pressing the CTRL key:
View online demo and code
HTML tag and, defining HTML forms Online
We create websites and individual pages on the Internet to communicate with visitors.
HTML forms are used to register visitors on the site, for interactive surveys and voting, allow you to send messages, make purchases, and so on. HTML The form is created for one purpose: collecting and subsequently transmitting information for processing by a software script or via email.
Example HTML form | Enter the site
Tags, attributes and values
- - determine the shape.
- name="" - defines the name of the form.
- method="" - defines the method of sending data from the form. Values: "get" (default) and "post" . The "post" method is often used, as it allows large amounts of data to be transferred.
- action="" - defines the url at which the data is sent for processing. In our case - enter_data.php ..
- - define such form elements as buttons, switches, text fields for data entry.
- type="text" - defines a text field for data entry.
- type="password" - defines a field for entering a password, with the text displayed in the form of asterisks or circles.
- type="checkbox" - defines a radio button.
- type="hidden" - defines a hidden form element - used to transfer additional information to the server.
- size="25" - length of the text field in characters.
- maxlength="30" - the maximum allowed number of entered characters.
- value="" - defines the value that will be sent for processing if it relates to radio buttons or switches. The value of this attribute as part of a text field or button will be shown as, for example, Vasya or Login in the example above.
Example HTML form | Comments on the site
|