Open In App

Get the Value of Text Input Field using JavaScript

Last Updated : 13 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In web development, retrieving the value entered into a text input field is a common task. Whether for form submission, validation, or dynamic content updates, knowing how to get the value of a text input field using JavaScript is essential.

What is a Text Input Field?

A text input field is a form element on a webpage where users can type in text. This is commonly seen in login forms, search bars, or any other place where user input is needed.

Syntax

<input type="text" id="myTextInput" />

In the above syntax

  • <input>: Defines an input field where users can enter data.
  • type="text": Specifies that the input field is for text (it allows users to type text).
  • id="myTextInput": Assigns a unique identifier (id) to the input field, making it easier to reference in JavaScript or CSS.
  • />: Self-closing tag, meaning no need for a closing </input>.

Accessing the Value of a Text Input Field

To retrieve the value entered into a text input field, follow these steps:

1. Select the Input Field

First, the input field must be selected using JavaScript. This can be done using methods like getElementById() or querySelector(). These methods allow access to the DOM (Document Object Model) elements on the page.

<input type="text" id="myTextInput" />

The JavaScript code to select this input field would be:

var textInput = document.getElementById('myTextInput');

Alternatively, we can use querySelector() to select the element:

var textInput = document.querySelector('#myTextInput');

2. Get the Value of the Input Field

After selecting the input field, access its value using the .value property. This property holds the current content of the input field, which can then be used in the code.

var inputValue = textInput.value;
console.log(inputValue);

Getting the Value of a Text Input Field

In JavaScript, to get the value of a text input field, we first need to select the input field using its id or other selectors. Once the field is selected, we can use the .value property to retrieve the text entered by the user.

HTML
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Using Value Property</title>
</head>
<body>
    <input type="text" id="myInput" placeholder="Enter text">

    <button onclick="getValue()">Get Value</button>

    <script>
        function getValue() {
            let inputField = document.getElementById("myInput");

            let value = inputField.value;
            alert("Input value: " + value);
        }
    </script>

</body>
</html>

 

Output:

valueeee

In this example

  • Input Field: <input type="text" id="myInput" placeholder="Enter text"> creates a text box for the user to type in.
  • Button: <button onclick="getValue()">Get Value</button> triggers the getValue() function when clicked.
  • JavaScript: let inputField = document.getElementById("myInput"); selects the input field using its id. let value = inputField.value; gets the text entered by the user.
  • alert("Input value: " + value); displays the value in an alert box.

Getting the Value of a Textarea

Text areas are commonly used when users need to input longer text, such as comments, descriptions, or messages. Just like with input fields, it's possible to get the value entered in a <textarea> using JavaScript.

HTML
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Textarea Value Example</title>
</head>
<body>
    <textarea id="myTextarea" 
              rows="4" cols="50" 
              placeholder="Enter text"></textarea>
    <button onclick="getValue()">Get Value</button>
    <script>
        function getValue() {
            let textarea = document.getElementById("myTextarea");

            let value = textarea.value;
            alert("Textarea value: " + value);
        }
    </script>
</body>
</html>

Output:

valueeee2

In this example

  • Textarea: <textarea id="myTextarea" rows="4" cols="50" placeholder="Enter text"></textarea> creates a multiline input field.
  • Button: <button onclick="getValue()">Get Value</button> triggers the getValue() function when clicked.
  • JavaScript: getValue() gets the text from the textarea using .value and shows it in an alert.

Getting the Input Value using an Event Listener

One common approach is using an event listener to capture the input when a user interacts with the field.

HTML
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Get Input Value Dynamically</title>
</head>

<body>
    <input type="text" id="myInput" placeholder="Start typing...">

    <script>
        document.getElementById('myInput').addEventListener('input', function () {
            let inputValue = this.value;
            console.log(inputValue);
        });
    </script>
</body>
</html>

Output

In this example

  • Input Field: <input id="myInput"> lets the user type text.
  • Event Listener: addEventListener('input', function) triggers when typing happens.
  • Get and Log Value: let inputValue = this.value; gets the typed text, and console.log(inputValue); logs it.

Getting the Input Value using the querySelector()

querySelector() is a versatile method in JavaScript used to select single HTML elements using CSS selectors.

HTML
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Get Input Value using querySelector()</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: flex-start;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }

        input {
            margin-top: 20px;
            text-align: center;
            padding: 10px;
            font-size: 16px;
            width: 300px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border: none;
            background-color: #007bff;
            color: white;
            border-radius: 5px;
        }

        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <input type="text" id="myInput" placeholder="Enter text here">
    <button onclick="getValue()">Get Value</button>
    <script>
        function getValue() {
            let inputValue = document.querySelector('#myInput').value;
            console.log(inputValue);
            alert("You entered: " + inputValue);
        }
    </script>
</body>
</html>

Output

In this example

  • HTML: An input field and a button are created. The button triggers a function when clicked.
  • CSS: Flexbox is used to center the input field and button at the top of the screen.
  • JavaScript: The getValue() function gets the input field’s value using querySelector() and displays it in an alert and the console when the button is clicked.

Conclusion

Retrieving the value of a text input field is an essential skill in web development, whether for dynamic updates, form submission, or validation. By using JavaScript methods like getElementById(), querySelector(), and event listeners, developers can easily access and manipulate user input


Get the value of text input field using JavaScript