How to Get Value of Textbox in JavaScript ?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this blog, I will show you how to get value of textbox in javascript from html. Now here we will know about how we can get value of textbox in javascript from html . We can simply use the value property of the DOM input element for get the value of any text input field.

So here I will give you two simple example of how to get value of input element in javascript. So now in this example I will do by using JavaScript to get the value from an HTML text box. So the first example is by using onclick and The second example is by using onchange.

Example 1 : Using onclick

<!DOCTYPE html>
<html>
<head>
    <title>how to get value of textbox in javascript - codingspoint.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 - codingspoint.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>

Read Also : How to Crop Image Before Upload using Cropper JS in Laravel

I hope it will help you. Also you can follow us onĀ Facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →