JQuery Set the Value of an Input Text Field
Last Updated :
08 Oct, 2024
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.
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 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 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
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.
Similar Reads
jQuery Tutorial jQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
What is Ajax ? Imagine browsing a website and being able to submit a form, load new content, or update information without having to refresh the entire page. That's the magic of AJAX. Asynchronous JavaScript and XML (AJAX) is a web development technique that allows web pages to communicate with a web server asynch
5 min read
Top 50+ jQuery Interview Questions and Answers - 2025 jQuery, a fast and lightweight JavaScript library, has been a game-changer in simplifying front-end web development known for its simplicity, ease of use, and cross-browser compatibility. In this article, we will provide the Top 50+ jQuery Interview Questions 2025 tailored for both freshers and expe
15+ 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
How to change Selected Value of a Drop-Down List using jQuery? We will change the default selected value of the dropdown using jQuery. In an HTML select tag, the default selected value is the value given to the first option tag. With jQuery, it is easy to change the selected value from a drop-down list. Below are the methods to change the selected value of a dr
3 min read
Form Validation using jQuery Form validation is a process of confirming the relevant information entered by the user in the input field. In this article, we will be validating a simple form that consists of a username, password, and a confirmed password using jQuery.Below are the approaches for validation of form using jQuery:T
5 min read
jQuery Introduction jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM). jQuery simplifies HTML document traversing and manipulation, browser event handling, DOM animations, Ajax interactions, and cross-browser Java
7 min read
jQuery UI Date Picker A date-picker of jQuery UI is used to provide a calendar to the user to select the date from a Calendar. This date picker is usually connected to a text box so user selection of date from the calendar can be transferred to the textbox. You need to add the below CDN to your HTML document to use it. C
3 min read
jQuery ajax() Method The jQuery ajax() method is used to perform asynchronous HTTP requests, allowing you to load data from a server without reloading the webpage. It provides a flexible way to interact with remote servers using GET, POST, or other HTTP methods, supporting various data formats.Syntax:$.ajax({name:value,
4 min read
jQuery Cheat Sheet â A Basic Guide to jQuery What is jQuery?jQuery is an open-source, feature-rich JavaScript library, designed to simplify the HTML document traversal and manipulation, event handling, animation, and Ajax with an easy-to-use API that supports the multiple browsers. It makes the easy interaction between the HTML & CSS docum
15+ min read