Home >Web Front-end >JS Tutorial >How Do I Set the Default Value of an Input Field Using JavaScript?
How to Set the Default Value of an Input Field
To define the default value that will appear in an input field when the page loads, you can use JavaScript to set the value attribute. This attribute specifies the initial value of the field.
Here's an example of how to do this:
document.getElementById("nameofid").value = "My value";
In this example, we're using the getElementById() method to access the input field with the id "nameofid." Then, we're setting its value property to "My value," which will be the default displayed text. You can replace "My value" with the desired default value for your field.
The above is the detailed content of How Do I Set the Default Value of an Input Field Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!