Code has been added to clipboard!

Input Text

Reading time 2 min
Published Sep 8, 2017
Updated Oct 10, 2019

JavaScript Input: Main Tips

  • The JavaScript input text property is used to set or return the value of a text input field.
  • The value property contains either the default value that is present upon loading the element, the value entered by the user, or the value assigned by the script.

textObject.value Property Explained

JavaScript .value property can help you manage content on your websites. For instance, forms and dropdown lists are appreciated elements that website developers include in their pages to communicate with their visitors or retrieve information.

The following code example shows how the JavaScript input property can modify the value of a text field:

Example
document.getElementById("sampleText").value = "John Bonham";

However, if you want to perform the JavaScript get input value task, you should remove everything that comes after equal sign. Then, you will not set the value but receive it.

Write it Correctly

We have two examples of get input value JavaScript usage. The first one holds a code which will return the value of the property. JavaScript get value of input:

textObject.value

The second example can be used for setting the value of the property. As you can see, it contains a value called text, indicating the value of the text input field:

textObject.value = text

Just like many other JavaScript properties, this one will also generates a return value. The JavaScript input text value property will produce a string, containing the value of the text field.

Useful Code Examples for Practice

If you are only starting to learn the get input .value JavaScript, we recommend that you start by practicing with the code examples we provide in this section. While the first one might seem rather long, it illustrates how form validation can be achieved:

Example
var email = document.getElementById("email").value.indexOf("@");
var age = document.getElementById("age").value;
var firstName = document.getElementById("firstName").value;   
Submitted = "true";
if (firstName.length > 10) {
    alert("The name must have no more than 10 characters");  
    Submitted = "false";
}
if (isNaN(age) || age < 12 || age > 100) {  
    alert("The age must be between numbers 12 and 100");  
    Submitted = "false";
}
if (at == -1) {  
    alert("The email address you entered is not valid"); 
    Submitted = "false";
}
if (Submitted == "false") {  
    return false;
}

If you click Try it Live button, you will see that the text input field contains a dropdown list:

Example
var sampleList = document.getElementById("sampleList");
document.getElementById("favoriteElem").value = sampleList.options[sampleList.selectedIndex].text;

This example shows a different type of dropdown list in the text input field. See for yourself and compare:

Example
var none = document.getElementById("none");
var option = none.options[none.selectedIndex].text;   
var sampleText = document.getElementById("resultElem").value;
sampleText = sampleText + option;   
document.getElementById("resultElem").value = txt;

Now, the following example illustrates how default value and value property differ:

Example
var textVal = document.getElementById("sampleText");
var defaultTextVal = textVal.defaultValue;
var currentTextVal = textVal.value;
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen