How to set default value in html

PHPz
Release: 2023-04-21 17:08:55
Original
5873 people have browsed it

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">
Copy after login

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 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
Copy after login

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