Home >Web Front-end >JS Tutorial >How to Set a Default Value for an Input Field Using JavaScript?

How to Set a Default Value for an Input Field Using JavaScript?

DDD
DDDOriginal
2024-12-07 07:08:16362browse

How to Set a Default Value for an Input Field Using JavaScript?

How to Set Default Value in Input Field Using JavaScript

Setting the default value of an HTML input text field in JavaScript is straightforward. To achieve this:

  1. Locate the Input Field:

    • Use document.getElementById('input-id') to retrieve the input field by its ID.
  2. Assign the Default Value:

    • Set the value property of the input field to the desired default value. For example:

      document.getElementById('default-value').value = 'My Default Value';

By following these steps, you can easily set the default value of an input text field in JavaScript, allowing users to quickly fill in forms with predefined information.

The above is the detailed content of How to Set a Default Value for an Input Field Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

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