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 options for selection are defined using 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

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 the meaning is different from the text. Once you select a color, jQuery displays the visible text in the alert. Tag code

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

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 - 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

    <a href="https://skillmaker.ru/en/news/rassuzhdeniya-o-semantike-koda-html-s-primerami-rassuzhdeniya-o-semantike-koda/">Example HTML</a> forms




    Name



    Mail








    Tags, attributes and values

    • action="http://site/comments.php" - defines the url to which data from the form will be sent.
    • id="" - defines the name and identifier of the form element.
    • name="" - defines the name of the form element.
    • - define a text field as part of the form.
    • cols="" - determines the number of columns of the form text field.
    • rows="" - defines the number of rows of the form text field.

    If between place text, it will be shown inside the field as an example that can be easily removed.

    Example HTML form | Drop-down list

    HTML forms




    Tags, attributes and values

    • - define a list with positions to select.
    • size="" - determines the number of visible list positions. If the value is 1 , we are dealing with a dropdown list.
    • - determine the positions (items) of the list.
    • value="" - contains the value that will be sent by the form to the specified url for processing.
    • selected="selected" - highlights a list item as an example.

    Example HTML form | List with scroll bar

    By increasing the value of the size="" attribute, we get a list with a scroll bar:

    First position Second position Third position Fourth position

    HTML forms




    For this option, use the multiple="multiple" flag, which makes it possible to select multiple positions. Its absence allows you to select only one item.

  • type="submit" - defines a button.
  • type="reset" - defines a reset button.
  • value="" - defines the label on the button.
  • See additionally:

    Thanks to the tag must be placed in the form (tag

    ). Below is an example:




    What are the attributes of the select tag?

    Tag







    The second attribute is required , with its help you can “tell” the browser that the list must be selected before submitting the form, otherwise the browser will prohibit submitting the form and will display a corresponding message to you. The appearance of this message is entirely browser dependent and cannot be changed by the user. Below is an example of using the required attribute:




    The third attribute is size , which can be used to specify the number of list items to display. The size attribute can only contain an integer. The size attribute transforms the list, for example, if it is equal to 1, then the tag








    Greetings to all readers of my blog. Today I will tell you how to make a drop-down list in HTML, what tags and attributes you need to use, and also for what purposes you may need it.

    The select tag and creating a dropdown list

    So, a drop-down list in html is created using a paired select tag, in which paired option tags are placed. It is in them that all the options that will be offered when you click on the list are recorded. Example:

    Choose an animal

    In this case, you will see what is displayed between the opening and closing parts of the option on the screen, and the value contained in the value attribute will be sent to the server or processed using a script.

    The list in html can be regular or with multiple choice. To make it possible to select multiple items, you need to add an empty multiple attribute to select. To select multiple values, hold ctrl and press the left mouse button.

    Another useful attribute is size . It allows you to choose how many rows to show in the dropdown list.

    Another attribute is disabled . It makes the list unclickable and unclickable, but still visible on the page.

    Required – html5 attribute. If set, the form will not be submitted without selecting a value from the dropdown list. In general, it becomes a must-have choice. While the attribute does not work in all browsers.

    Option Tag Attributes

    Actually, select only serves as a container for list items; they themselves are specified using the option tag. It has a value parameter, as we already found out, but besides this there are others. For example:
    Disabled – makes the list item unavailable for selection. It will be displayed, but you cannot click on it.

    Selected – the element will be selected by default. In a regular list, this attribute without a value can be assigned to only one item; in a multiple list, it can be assigned to several.

    Important clarification for proper operation

    If the selection result needs to be sent to the server or processed through scripts, then place the select in the form so that no errors occur. The fact is that the drop-down list was originally created as one of the form elements.

    What is select used for?

    Usually it can be useful to you if you register on your website or want to conduct a survey. The element has a drawback - it does not change very well appearance via css.

    By default, when you click on a list, a blue frame appears; those list items over which the cursor is hovered are highlighted in the same color. To prevent the frame from appearing when clicked, or to be highlighted in a different color, you can write the following selector:

    Select:focus(
    outline: 1px solid orange;
    }

    The frame will now be orange.

    Option can also be styled, but when you hover over an item, its background turns blue and for some reason this does not change through styles.

    Option(
    color: brown;
    background: aqua;
    }

    By the way, maybe some of you know how to change the background color when hovering over an item via css?

    That's all for now on the select tag and its use. None additional features there is no provision for it in html. All other operations with it can be performed using using javascript and php. For example, if you need to create a list to select the year of birth and there may be 80-100 different options, you won’t write them manually, will you?

    This is precisely why you need to use programming, namely a loop. Well, okay, this is a topic for another conversation, but for today this is all the information I wanted to tell you. You can get acquainted with other main tags in html.

    Description

    HTML tag may contain two or more tags

    The width of the dropdown will be determined by the longest text specified in the tag

    Attributes

    autofocus: Specifies that the element should automatically receive focus when the web page loads. Possible values ​​for the autofocus boolean attribute: Example » form: Defines the form the element is associated with. The attribute value is the element identifier
    . This attribute allows you to place an element Example »

    Note: due to in various ways selecting multiple items and additionally notifying users that multiple options are available, it is recommended to use checkboxes instead of a drop-down list.

    Name: Defines the name for the dropdown list. It can be used to access form data after it has been submitted, or to reference an element in JavaScript.

    size: Specifies the number of visible options in the dropdown list. If the value of the size attribute is greater than 1 but less than the total number of options in the list, then the browser will automatically add a scroll bar to indicate that there are more options to view.