All TalkersCode Topics

Follow TalkersCode On Social Media

How To Get Text Box Value In JavaScript

Last Updated : Jan 1, 2023

How To Get Text Box Value In JavaScript

In this article we will show you the solution of how to get text box value in JavaScript, JavaScript can interact with distinct web pages to give server as well as client- side services.

The objective of JavaScript is to give interactive essentials on web pages to engage users.

These interactive essentials include dynamic styling, inputting the value, getting value from forms, etc. We can get the value of the text input field using various styles in JavaScript

Step By Step Guide On How To Get Text Box Value In JavaScript :-

There are numerous types of input fields, and while getting their value is served also in each case, it requires some reflection to serve smoothly.

How to Get the value of text input

The input’s value will be published to the press when the getVal() function is invoked.

Every input can admit an trait that establishes a “call to action” – in other words, an event that triggers the performance of a function.

Each type of input will bear distinct events to trigger the handler function – that’s the part of reacquiring input from the user that requires some thinking.

On top of this, multiple events can be utilized with the same input. Understanding when these events are touched off, and how they interact with your input field, is the key to getting your event instructors working duly.

The onblur event triggers the getVal() function once the field loses core( i.e. the user navigates down from the field).

Event Handler Syntax

Working with event instructors appropriately requires trouble at both the DOM level and at the Script level.

1. DOM level

Make assured that the DOM element has an event property that triggers your event handler function.

Make assured that the DOM event you ’ve taken is the accurate selection for your input.


To define your event handler, append it as an property on the element utilizing the following format

DOMevent = ” funcName() ”

2. Script level

At the script level, exclusively define your handler function

function funcName()
const val = findHTMLelement.value

There are different ways to develop the value from the input field by applying JavaScript:

  • Using the getElementById in JavaScript
  • Using getElementsByClassName in JavaScript

Technique 1 Using the getElementById in JavaScript -

In JavaScript, the getElementById technique is applied to get the input text box value with an ID property. This technique is placed to return an output by a specified value.

It reverts the null value if the element isn't present then. maximum users use it to read and modify HTML essentials. The syntax of the getElementById is given below



In this syntax, the getElementById technique extracts the value by passing the indistinguishable ID property “ inputId ”.

Technique 2 Using the getElementsByClassName in JavaScript -

In JavaScript, another technique known as getElementsByClassName is utilized to get the value of the text input field. It returns the set of essentials specified by the class name.

The getElementsByClassName() technique is called utilizing the element of document.

It searches the entire document and gives the output of all child essentials present in the document. The Syntax to use this technique is delivered below:


The syntax is described as:

1. inputClass shows the name of the class.

2. [0] It represents that if no matching element is present then, also return undetermined.

<!DOCTYPE html>
<body style="text-align:center;">
 <h1 style="color:purple;">
 Name:<input type="text" id="myText" value="">
 <button type="button" onclick="myFunction()">Click</button>
 <p id="demo"></p>
 function myFunction() {
  var a = document.getElementById("myText").value;
  document.getElementById("demo").innerHTML = a;
  1. In the starting lines of code we opens the <html> tag.
  2. After that we start the body by giving some styling property using heading tag.
  3. Then we write the statement which we want to display on our page.
  4. And we provides the value to it.
  5. Then we create the button for getting our text using javacsript function.
  6. After that our script starts in which we create our function named as myfunction()
  7. In that function we store the value of variable which we want to print using document.getElementById.
  8. Then we close our script.
  9. Also we close body and html.

Conclusion :-

Hence, we successfully learnt that how to get text box value in JavaScript.

I hope this article on how to get text box value in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials