How to Get Value of Textbox in JavaScript

JavaScript

Nicesnippets

417

24-01-2020


Hi Guys,

Today, I will show you how to get value of textbox in javascript from html. You can simply use the value property of the DOM input element to get the value of text input field.

I will give you two simple example of get value of input element in javascript. In this example I will using JavaScript to get the value from an HTML text box. The first exmaple in using onclick and The second exmaple in using onchange.

Example 1 : Using onclick

<!DOCTYPE html>

<html>

<head>

<title>how to get value of textbox in javascript - nicesnippets.com</title>

</head>

<body>

<form action="" method="">

<label>Name :-</label>

<input type="text" name="name" placeholder="Enter Name" id="name">

<button type="button" onclick="getNameValue();">Save</button>

</form>

<script type="text/javascript">

function getNameValue(){

var name = document.getElementById("name").value;

alert(name);

}

</script>

</body>

</html>

Example 2 : Using onchange

<!DOCTYPE html>

<html>

<head>

<title>how to get value of textbox in javascript - nicesnippets.com</title>

</head>

<body>

<form action="" method="">

<label>Name :-</label>

<input type="text" name="name" placeholder="Enter Name" id="name" onchange="getNameValue();">

</form>

<script type="text/javascript">

function getNameValue(){

var name = document.getElementById("name").value;

alert(name);

}

</script>

</body>

</html>

It will help you...


Recommended Posts