Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to take user input in JavaScript?
Next article icon

Set the Value of an Input Field in JavaScript

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We will learn how you can set the value of an input field in JavaScript. We will set user input to the placeholder value of the input field using JavaScript.

Below are the approaches to set the value of an input field in JavaScript:

Table of Content

  • By using the innerHTML property
  • By using setAttribute Method

By using the innerHTML property

  • In this approach, we are going to call a function when the user clicks the button.
  • The click event will occur and a function will be invoked.
  • The function will get the input value from the input field and add that value using the “innerHTML” property provided by JavaScript.
  • At last, that value will be displayed on the screen.

Example: This example is the implementation of the above-explained approach.

html
<!DOCTYPE html> <html>  <head>     <title>         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>         let el_down = document.getElementById("GFG_DOWN");         let inputF = document.getElementById("id1");          function gfg_Run() {             inputF.value = "textValue";             el_down.innerHTML =                 "Value = " + "'" + inputF.value + "'";         }     </script> </body>  </html> 

Output:

Set the value of an input field

Set the value of an input field

By using setAttribute Method

  • In this approach, we are going to call a function when the user clicks the button.
  • The click event will occur and a function will be invoked.
  • The function will get the input value from the input field and set that value using the setAttribute() method.
  • And add that value using the “innerHTML” property provided by JavaScript.
  • At last, that value will be displayed on the screen.

Example: This example is the implementation of the above-explained approach.

html
<!DOCTYPE html> <html>  <head>     <title>         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>         let el_down = document.getElementById("GFG_DOWN");         let inputF = document.getElementById("id1");          function gfg_Run() {             inputF.setAttribute('value', 'defaultValue');             el_down.innerHTML =                 "Value = " + "'" + inputF.value + "'";         }     </script> </body>  </html> 

Output:

Set the value of an input field

Set the value of an input field



Next Article
How to take user input in JavaScript?

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies

Similar Reads

  • Get the Value of Text Input Field using JavaScript
    Getting the value of a text input field using JavaScript refers to accessing the current content entered by a user in an input element, such as <input> or <textarea>, by utilizing the value property. This allows for dynamic interaction and data manipulation in web forms. Syntax inputFiel
    2 min read
  • JQuery Set the Value of an Input Text Field
    Set the value of an input text field in jQuery is useful while working with forms. It is used to add new value to the input text field. In jQuery, there are various methods to set the value of input text fields, these are - val(), prop(), and attr(). These method offers unique capabilities for setti
    3 min read
  • JavaScript - How to Place Cursor at End of Text in Text Input Field?
    Here are the various methods to place cursor at the end of text in text input field in JavaScript Using HTMLInputElement.setSelectionRange() MethodThe setSelectionRange() method is the most basic way to move the cursor within a text input or textarea. It allows you to set the start and end positions
    1 min read
  • How to take user input in JavaScript?
    Interacting with users is the best way to create engaging and dynamic web applications. Taking user input allows your applications to be interactive and responsive. Here we will see the approach to take user input in JavaScript, specifically using the prompt method, which is perfect for beginners. A
    3 min read
  • How to get hidden field array values in javascript?
    To retrieve values from hidden fields in JavaScript, you can use methods like querySelectorAll or getElementsByName to select these elements and extract their values. This can be useful for gathering data that isn't directly visible on a web page but still needs to be accessed or processed. Example
    2 min read
  • How to Add an Input Field on Button Click in JavaScript ?
    JavaScript allows us to dynamically add an input field upon button click. We can use the below approaches for adding an input field on Button Click in JavaScript. Table of Content Using DOM ManipulationUsing Template Literal and InnerHTMLUsing DOM ManipulationDOM manipulation is used to dynamically
    2 min read
  • How to add readonly attribute to an input tag in JavaScript ?
    Use the setAttribute() Method to add the read-only attribute to the form input field using JavaScript. setAttribute() Method This method adds the defined attribute to an element and gives it the defined value. If the specified attribute is already present, then the value is being set or changed. Syn
    2 min read
  • How to Get the Value of an Input Text Box using jQuery?
    When working with forms or interactive web pages, sometimes need to get the values entered into the input fields. This article provide a complete guide on how to get the value o an input text box using jQuery. Below are the different approaches to get the value of an input text box using jQuery: Get
    2 min read
  • How to force Input field to enter numbers only using JavaScript ?
    We are going to learn how can we force the Input field to enter numbers only using JavaScript. Below are the approaches to force input field force numbers using JavaScript: Table of Content By using ASCII ValueBy using replace(), isNan() functionBy using ASCII ValueIn this approach, we are going to
    3 min read
  • How to set the input field value dynamically in AngularJS ?
    Given an input field and the task is to set the input field value dynamically with the help of AngularJS. Approach: A value is being set to the input field despite the user enter something or not. ng-click event is used to call a function that sets the value to 'This is GeeksForGeeks' with the help
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences