How to get the value of text input field using JavaScript?

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>

    chevron_right

    
    

    Output:

    • Before click on button:

    • After clicking we get the value:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.