Sometimes we need to set a default value of the <input> element, This example explains methods to do so.
- Text Value Property
This property set/return the value of value attribute of a text field.
The value property contains the default value, the value a user types or a value set by a script.
Syntax:-
Return the value property:
textObject.value
-
Set the value property:
textObject.value = text
Property Values:
- text:It specifies the value of input text field.
- attributeValue:This parameter is required. It specifies the value of the attribute to add.
-
Return the value property:
- setAttribute method
This method adds the specified attribute to an element, and set it’s specified value.
If the attribute already present, then it’s value is set/changed.
Syntax:element.setAttribute(attributeName, attributeValue)
Parameters:
- attributeName:This parameter is required. It specifies the name of the attribute to add.
- attributeValue:This parameter is required. It specifies the value of the attribute to add.
Example-1:This example sets the value of the input element to ‘textValue’ by Text Value property.
<!DOCTYPE html> <html> <head> <title> JavaScript | Set the value of an input field. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <input type='text' id='id1' /> <br> <br> <button onclick="gfg_Run()"> click to set </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_down = document.getElementById("GFG_DOWN"); var inputF = document.getElementById("id1"); function gfg_Run() { inputF.value = "textValue"; el_down.innerHTML = "Value = " + "'" + inputF.value + "'"; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-2:This example sets the value of the input element to ‘defaultValue’ by setAttribute method.
<!DOCTYPE html> <html> <head> <title> JavaScript | Set the value of an input field. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <input type='text' id='id1' /> <br> <br> <button onclick="gfg_Run()"> click to set </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_down = document.getElementById("GFG_DOWN"); var inputF = document.getElementById("id1"); function gfg_Run() { inputF.setAttribute('value', 'defaultValue'); el_down.innerHTML = "Value = " + "'" + inputF.value + "'"; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to validate if input in input field exactly equals to some other value using express-validator ?
- How to validate if input in input field has boolean value using express-validator ?
- How to get the value of text input field using JavaScript?
- JQuery | Set the value of an input text field
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input in input field has alphanumeric characters only using express-validator ?
- How to validate if input in input field has base64 encoded string using express-validator ?
- How to validate if input in input field has ASCII characters using express-validator ?
- How to validate if input in input field has base 32 encoded string using express-validator ?
- How to validate if input in input field has alphabets only using express-validator ?
- How to validate if input in input field has BIC or Swift code only using express-validator ?
- How to validate if input in input field has integer number only using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- How to validate if input in input field has decimal number only using express-validator ?
- How to validate if input in input field is a valid date using express-validator ?
- How to validate if input in input field has lowercase letters only using express-validator ?
- How to validate if input in input field is divisible by or multiple of a number using express-validator ?
- How to validate if input in input field is a valid credit card number using express-validator ?
- How to validate if input in input field has uppercase letters only using express-validator?
- How to validate if input in input field has float number only using express-validator ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


