How to get the value of text input field using JavaScript?
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:
- JQuery | Set the value of an input text field
- JQuery | Set focus on a form input text field on page load
- How to select all text in HTML text input when clicked using JavaScript?
- Set the value of an input field in JavaScript
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- HTML | Clearing the input field
- How to validate input field while focusout ?
- How to disable browser Autocomplete on web form field/input tag?
- JavaScript | Auto-filling one field same as other
- HTML | DOM Input Text name Property
- HTML | DOM Input Text value Property
- HTML | DOM Input Text Object
- HTML | <input type="text">
- How to make text input non-editable using CSS?
- HTML | DOM Input Text autofocus Property
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.



