We can get the value of text input field using various methods in script. There is a text value property which can set and return the value of the value attribute of a text field. Also we can use jquery val() method inside script to get or set the value of text input field.
- Using text value property:
Syntax:Get value : textObject.value Set value : textObject.value = text
Example-1: This example uses Text value property to get the value from input text field
<!DOCTYPE html><html><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><h2>Text value property</h2><p>Change the text of the textfield, and then click the button below.</p>Name:<inputtype="text"id="myText"value="Mickey"><buttontype="button"onclick="myFunction()">Try it</button><pid="demo"></p><script>// Here the value is stored in new variable xfunction myFunction() {var x =document.getElementById("myText").value;document.getElementById("demo").innerHTML = x;}</script></body></html>chevron_rightfilter_noneOutput :
- Before change in field:

- After input text and click:
- Before change in field:
-
Using jquery val() method:
The val() method is used to return or set the value attribute of the selected elements. In default mode this method returns the value of the value attribute of the FIRST matched element & sets the value of the value attribute for ALL matched elements.Syntax:
Get value : $(selector).val() Set value : $(selector).val(value)
Example-2:
<!DOCTYPE html><html><head><scriptsrc=</script><script>$(document).ready(function() {$("button").click(function() {//here the value is stored in variable.var x = $("input:text").val();document.getElementById("demo").innerHTML = x;});});</script></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><h2>jquery val() method</h2><p>Name:<inputtype="text"name="user"value="geeksforgeeks"></p><button>Get the value of the input field</button><pid="demo"></p></body></html>chevron_rightfilter_noneOutput:
- Before click on button:
- After clicking we get the value:
- Before click on button:
Recommended Posts:
- 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 boolean value using express-validator ?
- JQuery | Set the value of an input text field
- How to validate if input date (end date) in input field must be after a given date (start date) 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 has base64 encoded string 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 base 32 encoded string 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 BIC or Swift code only using express-validator ?
- How to validate if input in input field has integer number only using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- How to validate if input in input field has decimal number only using express-validator ?
- How to validate if input in input field is a valid date using express-validator ?
- How to validate if input in input field has lowercase letters only using express-validator ?
- How to validate if input in input field is divisible by or multiple of a number using express-validator ?
- How to validate if input in input field is a valid credit card number using express-validator ?
- How to validate if input in input field has uppercase letters only using express-validator?
- How to validate if input in input field has float number only using express-validator ?
- How to validate if input in input field must contains a seed word using express-validator ?
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.


