Home > Web Front-end > JS Tutorial > How Do I Set the Default Value of an Input Field Using JavaScript?

How Do I Set the Default Value of an Input Field Using JavaScript?

Patricia Arquette
Release: 2024-12-09 20:39:10
Original
900 people have browsed it

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

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template