how to get textbox value using javascript?

You can easily get textbox or form elements value using javascript. You can use one of the the following method to get the textbox value

  • document.getElementById
  • document.getElmentsByTagName
  • document.getElementsByName
  • document.getElementsByClassName
  • document.reg_frm.fname ¬†//where reg_frm is your form name; fname is your textbox name


<!DOCTYPE html>

<form name="reg_frm">

First Name: <input type="text" name="fname" class="myclass" id="myText" value="CODEFORBEGINNER">

<p>Click the button to display the value of the value attribute of the text field.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

function myFunction() {

//use any one method to get output

var x = document.getElementById("myText").value;
var x = document.getElementsByName("fname")[0].value;
var x = document.getElementsByTagName("input")[0].value;
var x = document.getElementsByClassName("myclass")[0].value;
var x = document.reg_frm.fname.value;
document.getElementById("demo").innerHTML = x;