Skip to content
Related Articles

Related Articles

Improve Article
How to get the value of text input field using JavaScript?
  • Difficulty Level : Hard
  • Last Updated : 16 Apr, 2019

We can get the value of text input field using various methods in script. There is a text value property which can set and return the value of the value attribute of a text field. Also we can use jquery val() method inside script to get or set the value of text input field.

  • Using text value property:
    Syntax:
    Get value : textObject.value
    Set value : textObject.value = text
    

    Example-1: This example uses Text value property to get the value from input text field




    <!DOCTYPE html>
    <html>
      
    <body style="text-align:center;">
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1>
      
        <h2>
          Text value property    
      </h2>
      
        <p>
          Change the text of the text 
          field, and then click the button below.
      </p>
      
        Name:
        <input type="text"
               id="myText"
               value="Mickey">
      
        <button type="button" 
                onclick="myFunction()">
          Try it
      </button>
      
        <p id="demo"></p>
      
        <script>
            // Here the value is stored in new variable x 
      
            function myFunction() {
                var x = 
                    document.getElementById("myText").value;
                
                document.getElementById(
                  "demo").innerHTML = x;
            }
        </script>
    </body>
      
    </html>

    Output :

    • Before change in field:



    • After input text and click:
  • Using jquery val() method:
    The val() method is used to return or set the value attribute of the selected elements. In default mode this method returns the value of the value attribute of the FIRST matched element & sets the value of the value attribute for ALL matched elements.

    Syntax:

    Get value : $(selector).val()
    Set value : $(selector).val(value)
    

    Example-2:




    <!DOCTYPE html>
    <html>
      
    <head>
        <script src=
      </script>
      
        <script>
            $(document).ready(function() {
      
                $("button").click(function() {
                    
                  //here the value is stored in variable. 
                    var x = $("input:text").val(); 
      
                    document.getElementById("demo").innerHTML = x;
                });
      
            });
        </script>
      
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1>
      
        <h2>jquery val() method
      
    </h2>
        <p>Name:
      
            <input type="text"
                   name="user" 
                   value="geeksforgeeks">
      
        </p>
      
        <button>
          Get the value of the input field
      </button>
      
        <p id="demo"></p>
      
    </body>
      
    </html>

    Output:

    • Before click on button:

    • After clicking we get the value:



My Personal Notes arrow_drop_up
Recommended Articles
Page :