There is a Input Element, the task is to set its value using JQuery. Here are a few examples discussed.
To understand example first few methods to know.
JQuery val() method:
This method return/set the value attribute of selected elements.
If we use this method to return value, it will return the value of the FIRST selected element.
If we use this method to set value, it will set one or more than one value attribute for set of selected elements.
Syntax:
-
Return the value attribute:
$(selector).val()
-
Set the value attribute:
$(selector).val(value)
-
Set the value attribute using function:
$(selector).val(function(index, curValue))
Parameters:
- value:This parameter is required. It specifies the value of value attribute.
-
function(index, currentValue):This parameter is optional. It specifies the function that returns the value to set.
- index: It returns the position of index of element in set.
- currentValue: It returns the current value attribute of the selected elements.
Example 1: In this example the value of the input element is set by val() method by selecting the input element from its ID.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Set value of input text. </title> <script src= </script> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksForGeeks </h1> Input Box: <input id="input" type="text" class="Disable" value="" /> <br> <br> <button id="setText"> setText </button> <script> $("#setText").click(function(event) { $('#input').val("GeeksForGeeks"); }); </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: In this example the value of the input element is set by val() method by selecting the input element from its parent element < body > and then selecting the < input > element.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Set value of input text. </title> <script src= </script> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksForGeeks </h1> Input Box: <input id="input" type="text" class="Disable" value="" /> <br> <br> <button id="setText"> setText </button> <script> $("#setText").click( function(event) { $('body input').val( "GeeksForGeeks"); }); </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JQuery | Set focus on a form input text field on page load
- How to get the value of text input field using JavaScript?
- How to put a responsive clear button inside HTML input text field ?
- How to detect value change on hidden input field in JQuery ?
- How to get the value in an input text box using jQuery ?
- How to make a text input non-editable using jQuery ?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- How to validate if input in input field has base64 encoded string 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 BIC or Swift code only 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 alphanumeric characters only using express-validator ?
- 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 alphabets only using express-validator ?
- How to validate if input in input field has boolean value using express-validator ?
- How to select all text in HTML text input when clicked using JavaScript?
- Set the value of an input field in JavaScript
- Semantic-UI | Input Field
- How to validate input field while focusout ?
- HTML | Clearing the input field
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.

