Home > Article > Web Front-end > How to set default value in html
HTML is Hypertext Markup Language, which is the basic language used to create web pages. A web page usually includes three languages: HTML, CSS and JavaScript. Among them, HTML is the most basic language, which is used to describe the text content, structure and layout of web pages.
When we design a web page, we sometimes need to set the default values of some controls, such as input boxes, drop-down boxes, radio buttons, check boxes, etc. The default value of these controls is the value displayed by these controls when the user accesses the web page. If no default value is set, these controls are usually blank.
So, how to set the default value of a control in HTML? Next, let us find out together.
1. Setting the default value of the input box in HTML
In HTML, you can set the default value of the input box by setting the value attribute in the tag. For example:
<input type="text" name="username" value="admin">
In the above code, the name attribute indicates that the name of the input box is "username", and the value attribute indicates that the default value of the input box is "admin".
2. Setting the default value of the drop-down box in HTML
In HTML, you can set the default value of the drop-down box by setting the selected attribute in the
<select name="fruit"> <option value="apple" selected>Apple</option> <option value="banana">Banana</option> <option value="orange">Orange</option> </select>
In the above code, the first
3. Setting the default values of radio buttons and check boxes in HTML
The default values of radio buttons and check boxes can be set by setting the checked attribute in the tag to fulfill. For example:
<input type="radio" name="gender" value="male" checked> Male <input type="radio" name="gender" value="female"> Female <input type="checkbox" name="hobby" value="reading" checked> Reading <input type="checkbox" name="hobby" value="traveling" checked> Traveling <input type="checkbox" name="hobby" value="sports"> Sports
In the above code, the first radio button and the first two check boxes have the checked attribute set, indicating that they are selected by default. Note that the name attributes of the radio button and check box must be the same, otherwise they will not be in the same option group.
4. Setting the default value of the text area in HTML
The text area is a multi-line input box, and its default value can be achieved by setting the text content in the
<textarea name="description">This is a description.</textarea>
In the above code, the default value of the text field is "This is a description.".
Summary
Through the above method, we can set the default value of the control in HTML, so that when the user accesses the web page, these controls will display the value we set by default. This can facilitate users and improve user experience.
It should be noted that setting default values does not mean that users must enter these values. Users can still modify the values of these controls at will.
The above is the detailed content of How to set default value in html. For more information, please follow other related articles on the PHP Chinese website!