The Wayback Machine - https://web.archive.org/web/20240901120309/https://www.geeksforgeeks.org/how-to-get-the-value-of-text-input-field-using-javascript/
Open In App

Get the value of text input field using JavaScript

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Getting the value of a text input field using JavaScript refers to accessing the current content entered by a user in an input element, such as <input> or <textarea>, by utilizing the value property. This allows for dynamic interaction and data manipulation in web forms.

Syntax

inputField.value;
inputField.value = text;

Getting the Value of a Text Input Field

The value property in JavaScript represents the current value of an input element, such as <input>, <textarea>, or <select>. It allows you to access and manipulate the text, number, or selected option within the input element.

Example: Here, we are using the Value property to get the value of the text input field.

  • An <input> element with the ID “myInput” is defined.
  • A button with an onclick event handler is provided to trigger the getValue() function.
  • The getValue() function retrieves the input element using getElementById(), accesses its value property to get the text entered by the user, and displays it in an alert.
HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Using Value Property</title>
</head>

<body>

    <!-- Input field -->
    <input type="text" id="myInput" placeholder="Enter text">

    <!-- Button to get input value -->
    <button onclick="getValue()">Get Value</button>

    <!-- Script to get input value -->
    <script>
        function getValue() {
            // Get the input element by its ID
            let inputField = document.getElementById("myInput");

            // Get the value of the input field
            let value = inputField.value;

            // Display the value in an alert
            alert("Input value: " + value);
        }
    </script>

</body>

</html>

 

Output:

valueeee

Getting the Value of a Textarea

Example : Here, we are getting the text area value using value property.

  • A <textarea> element with the ID “myTextarea” is defined.
  • A button with an onclick event handler is provided to trigger the getValue() function.
  • The getValue() function retrieves the <textarea> element using getElementById(), accesses its value property to get the text entered by the user, and displays it in an alert.
HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Textarea Value Example</title>
</head>

<body>

    <!-- Textarea -->
    <textarea id="myTextarea" 
              rows="4" cols="50" 
              placeholder="Enter text"></textarea>

    <!-- Button to get textarea value -->
    <button onclick="getValue()">Get Value</button>

    <!-- Script to get textarea value -->
    <script>
        function getValue() {
            // Get the textarea element by its ID
            let textarea = document.getElementById("myTextarea");

            // Get the value of the textarea
            let value = textarea.value;

            // Display the value in an alert
            alert("Textarea value: " + value);
        }
    </script>

</body>

</html>

Output:

valueeee2



Similar Reads

How to place cursor position at end of text in text input field using JavaScript ?
In this article, we are going to learn about how to place the cursor at the end of the text in a text input element using JavaScript.  At first, we are going to create a text input box where some value will be given and a button to place the cursor at the end. We can place the cursor at the end of the text in a text input element by using different
2 min read
How to validate if input in input field exactly equals to some other value using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type some exact value and based on that we give the user access to the request or deny the r
4 min read
How to validate if input in input field has boolean value using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only boolean value i.e. true or false are allowed. We can also validate these input fields to accept onl
4 min read
JQuery Set the value of an input text field
We will explore different methods to set the value of an input field using jQuery. By using jQuery's built-in methods, you can easily control and update input fields on any web page with just a few lines of code. We’ll cover three key methods: val(), prop(), and attr(), each of which offers unique capabilities for setting input values. Table of Con
3 min read
How to validate if input in input field has alphanumeric characters only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only alphanumeric characters are allowed i.e. there not allowed any special characters. We can also vali
4 min read
How to validate if input in input field has base64 encoded string using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. If In a certain input field only base 64 encoded string are allowed i.e there not allowed any other form of string which not const
4 min read
How to validate if input in input field has base 32 encoded string using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. If In a certain input field, only base 32 encoded strings are allowed i.e. there not allowed any other form of string which does n
4 min read
How to validate if input in input field has alphabets only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only alphabets are allowed i.e. there not allowed any number or special character. We can also validate
4 min read
How to validate if input in input field has BIC or Swift code only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. When working with a bank system, In a certain input field only a valid Bank identification code(BIC) is allowed. BIC is an 8 or 11
5 min read
How to validate if input in input field has integer number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only integer numbers are allowed i.e. there not allowed any strings, special characters, or anything oth
4 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
How to validate if input in input field has decimal number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only decimal numbers are allowed i.e. there not allowed any strings, special characters, or anything oth
4 min read
How to validate if input in input field is a valid date using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only a valid date is allowed i.e. there is not allowed any strings, number, or invalid date characters.
4 min read
How to validate if input in input field has lowercase letters only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only lowercase letters are allowed. We can also validate these input fields to accept only lowercase let
4 min read
How to validate if input in input field is divisible by or multiple of a number using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only numbers that are multiples of a particular number are allowed i.e. there not allowed any letters, s
5 min read
How to validate if input in input field is a valid credit card number using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only valid credit card numbers are allowed i.e. there not allowed any other string or number which not f
5 min read
How to validate if input in input field has uppercase letters only using express-validator?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only uppercase letters are allowed. We can also validate these input fields to accept only uppercase let
4 min read
How to validate if input in input field has float number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only float numbers are allowed i.e. there not allowed any strings, special characters or anything other
4 min read
How to validate if input in input field must contains a seed word using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, we often need to validate input so that it must contain a seed word i.e. input can be anything(any strin
5 min read
How to validate if input in input field has full width string only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only full-width strings are allowed. We can also validate these input fields to only accept full-width s
4 min read
How to validate if input in input field has hexadecimal number only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only hexadecimal numbers are allowed. We can also validate these input fields to only accept full-width
4 min read
How to validate if input in input field has valid hexadecimal color only using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only valid hexadecimal color codes strings are allowed. We can also validate these input fields to only
4 min read
How to validate if input in input field has ASCII characters using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only Ascii characters are allowed i.e. there is not allowed any Non-ASCII characters (Ex: ñ). We can als
4 min read
How to create Underline Input Text Field using CSS ?
Input Text Field can be underlined by implementing different approaches to applying a bottom border to an input text field, using various methods such as HTML and CSS. Styling input text fields is a crucial aspect of web development, and adding a bottom border is a common way to enhance the visual appeal of these elements. Table of Content Using CS
2 min read
How to apply bottom border with only Input text field using CSS ?
Adding a bottom border to the input field visually distinguishes it from surrounding elements on the page. Making input fields stand out on a website in a form for example is crucial. Styling these inputs with clear borders, appealing colors, and effects, can easily capture the user’s attention. Table of Content Using the outline and border-bottom
3 min read
Set the value of an input field in JavaScript
In JavaScript, you have several options for setting the value of an input field. This guide aims to offer a thorough explanation of these methods. Let's explore the various techniques for setting the value of an input field in JavaScript: Table of Content By using the innerHTML propertyBy using setAttribute Method 1. By using the innerHTML property
2 min read
Which input type is used to hide the input field to be displayed in HTML Form ?
The form tag is generally used to create forms for entering data. We can use different form tag attributes like text boxes, text area, checkboxes, radio buttons, buttons, dropdown list, etc. to collect data from the users. We need to use the HTML &lt;input type=”hidden”&gt; type for hiding the input field. Using this attribute, the input field will
1 min read
How to get the value in an input text box using jQuery ?
In this article, we will learn about the different methods to access the value of the input text box entered by the user using jQuery. Below are the different approaches listed to get the value of an input text box using jQuery: Table of Content Using the val() methodUsing the prop() methodLet us now discuss all the above-listed methods in detail w
3 min read
JQuery | Set focus on a form input text field on page load
The task is to set the focus on form input text field using jQuery. To do so, Here are a few of the most used techniques discussed. First focus() method needs to be discussed. jQuery focus() MethodThe focus event happens when an element gets focus (Either selected by a mouse click or by "tab-navigating" to it). This method triggers the focus event
2 min read
How to append an text message when an input field is modified in jQuery ?
In this article, we will see how to append an element with a text message when an input field is changed in jQuery. To append an element with a text message when the input field is changed, change() and appendTo() methods are used. The change() method is used to detect the change in the value of input fields. This method works only on the “&lt;inpu
2 min read