How to Get Value of Input in TypeScript ? Last Updated : 23 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In TypeScript, retrieving the value of an input element is a common task when building web applications. Whether you're handling form submissions, validating user input, or manipulating data based on user interactions, accessing input values is essential. The below approaches can be utilized to get input value in TypeScript: Table of Content Using document.getElementByIdRetrieving value in ReactUsing document.getElementByIdThis approach involves using Vanilla JavaScript to access input elements by their IDs and then retrieving their values using the value property. Syntax:const inputElement = document. getElementById("inputId") as HTMLInputElement;const value: retrievingValueType = inputElement.value;Example: The below example retrieves the entered input value in Vanilla JavaScript and displays it on the screen. HTML <!-- index.html file--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title> Username Input </title> </head> <body> <input type="text" id="username" placeholder="Enter your username" /> <button id="submitBtn"> Submit </button> <p id="result"></p> </body> </html> JavaScript //index.ts file let usernameInput: HTMLInputElement = document. getElementById('username') as HTMLInputElement; let result: HTMLParagraphElement = document. getElementById('result') as HTMLParagraphElement; let username: string; document.getElementById('submitBtn'). addEventListener('click', function () { username = usernameInput.value; if (username) { result.textContent = 'Entered Username: ' + username; } else { result.textContent = 'Input field can not be empty'; } }); Output: Retrieving value in ReactReact provides controlled components where input values are managed by state, facilitating easy access to input values. Syntax:const [username, setUsername] = useState<string>('');<input type="text" value={username} onChange={(e) => setUsername(e.target.value)} />Example: The below code will explain how you can get the value of input element in ReactJS. JavaScript import React, { useState } from 'react'; const App: React.FC = () => { const [username, setUsername] = useState<string>(''); const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { setUsername(e.target.value); }; return ( <div> <input type="text" value={username} onChange={handleInputChange} placeholder="Enter your username" /> <p>Username: {username}</p> </div> ); }; export default App; Output: Comment More infoAdvertise with us Next Article How to Iterate over Enum Values in TypeScript? N nikunj_sonigara Follow Improve Article Tags : TypeScript Similar Reads How to Iterate over Enum Values in TypeScript? Enums in TypeScript are a way to define a set of named constants. Sometimes we may need to iterate over all the values of an enum. There are several approaches to iterate over the enum values in TypeScript which are as follows: Table of Content Using a for...in loopUsing Object.values()Using a forâ¦o 2 min read How to Get a Variable Type in TypeScript Understanding how to effectively ascertain the type of a variable in TypeScript is important for maintaining type safety and ensuring code robustness. In this article, we'll explore various approaches to determine the type of a variable, ranging from basic JavaScript operators to TypeScript-specific 2 min read How to Get an Object Value By Key in TypeScript In TypeScript, we can get an object value by key by accessing the specific properties within the objects of the dynamic type. This can be done using Dot Notation, Bracket Notation, and Optional Chaining. In this article, we will explore all these approaches along with their implementation in terms o 5 min read How to Use Fetch in TypeScript ? In TypeScript, fetching the data from any server or API using fetch() API is not the same as we used to do it in Plain or Vanilla JavaScript. Let us see how we can use the fetch API to fetch data from any server or API in TypeScript.NOTE: Before using this code, please make sure that you have jQuery 4 min read How to Access Enum Values in TypeScript ? Enums are a feature in TypeScript that help organize collections of related values. Accessing these values efficiently is crucial for clean, maintainable code. This guide provides a straightforward overview of different methods to access enum values in TypeScript, including bracket notation, dot not 3 min read How to use express in typescript ? In this article, we will see how to use Express in TypeScript. The TypeScript is a superset of JavaScript that provides type notation with the JavaScript, so we can handle our server, and very scalable for the future. Express is web framework helps to create server-side handling with the help of Nod 2 min read Like