How to read value of a textbox in jQuery

reading value of textbox jquery

If you want to read the text from an input box like this in jQuery, you can follow one of the following approaches :

1. Using val() function

Here is the syntax :

var str = $('#txt_name').val();

Example :

<!DOCTYPE html>
<meta charset="UTF-8">
<title>Insert title here</title>
  $(document).ready(function() {
    $("button").click(function() {
  <p id="p1">Enter text :</p>
  <input type="text" id="txt1" />
  <br />
  <br />
  <button>Click me</button>

Output :

reading value of textbox jquery

Note :

You can set a new value in the textbox using the val() method by passing the new text to the val() method as shown below :

$("#txt1").val("new text")


2. Using prop(‘value’)

We can also use $(“#txt1”).prop(‘value’) to read the text from the textbox.

  $(document).ready(function() {
    $("button").click(function() {


3. JavaScript way

If you want to use JavaScript to get the value, you can use :



© 2017, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags