How To Set Default Value In Textbox In Html Using JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript HTML | Written & Updated By - Amruta
In this tutorial we will show you the solution of how to set default value in textbox in html using javascript, here we are going to show you two types of method to set default value in textbox.
In first type we using input tag attribute for set value on text box then another type is using ‘defaultValue’ property in javascript.
Both are simple and easy way to sets value on text box.
Step By Step Guide On How To Set Default Value In Textbox In HTML Using JavaScript :-
Here we defined two input tag elements for showing inserted default value in text boxes on webpage.
In first input tag element we specified attribute ‘value’ it will help us to set default value on input field.
Then in another input tag element we specified id attribute ‘inp’ for use this element on script block there we accessing this element by ‘getElementById()’ method and using ‘defaultValue’ property we sets default value in this input field.
<!DOCTYPE html> <html> <head> <title>SET DEFAULT VALUE IN TEXTBOX</title> </head> <body> <input type="text" value="Prawin"> <input type="text" id="inp"> <script> document.getElementById("inp").defaultValue="Dhanu"; </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here we defined two input tag for showing two type of method to set default value on input box. In first input element we defined ‘value’ attribute with value ‘Prawin’ and value attribute is always used to set some text on particular html element.
- At next input element we defined ‘id’ attribute for access this on script block there we using ‘getElementById()’ for access this input tag and for set default value we need to specify ‘defaultValue’ property with that and we sets value as ‘Dhanu’.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to set default value on input box using javascript.
When we executes program on browser we can see two input boxes with separate default input values of ‘Prawin Dhanu’.
If you’re need to modify them you have to just edit that specified name with your choice then we get updated result on webpage it will changed permanently and we can edit that value on webpage when your refresh it will comes to default value.
I hope this tutorial on how to set default value in textbox in html using javascript helps you and the steps and method mentioned above are easy to follow and implement.