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
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to get the value of a textarea in jQuery ?
Next article icon

JQuery Set the Value of an Input Text Field

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 setting the value of an input text fields.

Table of Content

  • Using jQuery val() Method
  • Using jQuery prop() Method
  • Using jQuery attr() Method

Let us now discuss all of these methods one by one in detail with the help of code examples.

Set the Value of an Input Text Field using jQuery val() Method

The jQuery val() method is used to set or return the value of an input text field. To set a value to an input field, simply pass the desired value as a string within the method. This approach is quick and effective for updating input fields dynamically.

Syntax

$(selector).val("value_to_be_set")

Example: Setting the value of an input text field using val() method by selecting the input element using its ID.

html
<!DOCTYPE HTML> <html>  <head>     <title>         Set the Value of an Input Field using jQuery     </title>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">     </script>  </head>  <body>     <h1>GeeksforGeeks</h1>      <label>Input Box:</label>     <input type="text" value="" id="inputField" />     <br><br>      <button id="setText">         Set Value to Input Field     </button>      <script>         $("#setText").click(function(event) {              $('#inputField').val("Welcome Geeks!");          });      </script> </body>  </html> 

Output

Set-Input-Value-jQuery

Set the Value of an Input Text Field using jQuery prop() Method

The prop() method is another way to set the value of an input field. This method is primarily used for properties like checked, disabled, or selected, but it can also be used to set the value property of an input field.

Syntax

$('selector').prop("value", "value_to_be_set");

Example: This example explains the use of the prop() method to set the value of an input text field.

html
<!DOCTYPE HTML> <html>  <head>     <title>         Set the Value of an Input          Field using jQuery     </title>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">     </script>  </head>  <body>     <h1>GeeksforGeeks</h1>      <label>Input Box:</label>     <input type="text" value="" id="inputField" />     <br><br>      <button id="setText">         Set Value to Input Field     </button>      <script>         $("#setText").click(function(event) {              $('#inputField').prop("value", "Welcome Geeks!");          });      </script> </body>  </html> 

Output

Set-Input-Value-jQuery

Set the Value of an Input Text Field using jQuery attr() Method

The attr() method can also be used to set the value of an input field. Though it is generally used for setting attributes like id, class, and src, it can also modify the value attribute of input elements.

Syntax:

 $('selector').attr("value", "value_to_be_set");

Example: This example implements the attr() method to set the value of an input text field.

HTML
<!DOCTYPE HTML> <html>  <head>     <title>         Set the Value of an Input         Field using jQuery     </title>     <script src=" https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">     </script>  </head>  <body style="margin: 50px 0 0 50px;">     <h1>GeeksforGeeks</h1>      <label>Input Box:</label>     <input type="text" value="" id="inputField" />     <br><br>      <button id="setText">         Set Value to Input Field     </button>      <script>         $("#setText").click(function(event) {             $('#inputField').attr("value", "Welcome Geeks!");          });      </script> </body>  </html> 

Output

Set-Input-Value-jQuery

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document. It is widely famous with it’s philosophy of “Write less, do more”. You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.



Next Article
How to get the value of a textarea in jQuery ?

P

PranchalKatiyar
Improve
Article Tags :
  • JQuery
  • Web Technologies
  • jQuery-Basics
  • jQuery-Questions

Similar Reads

  • Set the Value of an Input Field in JavaScript
    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 propertyBy using setAttribut
    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 get the value of a textarea in jQuery ?
    To get the value of a textarea in jQuery, you can use the val() method. This method is commonly used to retrieve the current value of form elements like textarea, input, and select. It works by either retrieving or setting the value of the selected element. If no input is provided, the method return
    2 min read
  • 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
  • How to get text value of a selected option in jQuery ?
    Introduction: In this article, we will see how we can use jQuery to get the text value from the selected option. While working on some form-related user interface, we often use the select option, and to use that, we will surely need those values that are being selected. Approach: We will use select
    2 min read
  • How to get value of textbox in jQuery ?
    The purpose of this article is to demonstrate how we can get the value of textbox using jQuery. A basic understanding of HTML, CSS, and jQuery is required. This can be done by using the jQuery val() method. val() Method: This method is used to set or return the value of the attribute for the selecte
    2 min read
  • How to make a Value/Text Input using jQuery Mobile ?
    jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Value/Text Input using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=”stylesheet” h
    1 min read
  • How to create a Text Input using jQuery Mobile ?
    jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be a Text Input using jQuery Mobile. Approach: First, we add the jQuery Mobile scripts needed for the project. <link rel=”stylesheet” hre
    1 min read
  • How to prevent a text field losing focus using jQuery ?
    In this article, we will learn how to prevent a textfield or an input from losing focus using jQuery. This can be used in situations where user validation is required. There are two approaches that can be taken: Approach 1: We will be using the jQuery bind(), val(), preventDefault() and focus() meth
    3 min read
  • How to detect value change on hidden input field in JQuery ?
    In the <input> element with the type="hidden", the change() method of jQuery is not automatically triggered. The change() method is only triggered automatically when the user himself changes the value of the field and not the developer. When the developer changes the value inside the hidden fi
    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