How to Get Textbox Value in javaScript

To get textbox value in javascript example; In this tutorial, you will learn how to get textbox value in javascript using js methods like getElementById(), getElementsByClassName(),

A text box is a rectangular area on the screen where you can enter text. When you press Enter while typing in a text field, either the cursor will jump to the next field or the value will be submitted. In HTML, a text field is defined by an <input> tag with the type “text.” A text area is defined by a <textarea> tag

Before you know how to get value from textbox in JavaScript, you must have knowledge of getElementById(), getElementsByClassName() methods of JavaScript. Because this javascript dom methods help to select the elements in document.

How to Get Textbox Value in javaScript

  • Method 1 – To Get Textbox Value in javaScript using getElementById and Function
  • Method 2 – To Get Textbox Value in javaScript using getElementsByClassName and Function

Method 1 – To Get Textbox Value in javaScript using getElementById and Function

In the following example, using the getElementById method with a function from JavaScript, the value of the testbox has been gated:

<!DOCTYPE html>
<html>
<head>
 <title>how to get textbox value in javascript - Tutsmake</title>
<script type="text/javascript">
   function myFunction(){
      var name = document.getElementById("name").value;
      alert(name);
   }
</script>
</head>
<body>

<form action="" method="">
    <label>Name :-</label>
    <input type="text" name="name" placeholder="Enter Name" id="name">
    <button type="button" onclick="myFunction();">Click</button>
</form>

</body>
</html>

Method 2 – To Get Textbox Value in javaScript using getElementsByClassName and Function

In the following example, using the getElementsByClassName method with a function from JavaScript, the value of the testbox has been gated:

<!DOCTYPE html>
<html>
<head>
 <title>how to get textbox value in javascript - Tutsmake</title>
<script type="text/javascript">
   function myFunction(){
      var name = document.getElementsByClassName("name").value;
      alert(name);
   }
</script>
</head>
<body>

<form action="" method="">
    <label>Name :-</label>
    <input type="text" name="name" class="name" placeholder="Enter Name" id="name">
    <button type="button" onclick="myFunction();">Click</button>
</form>

</body>
</html>

Recommended JavaScript Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *