jQuery | val() with Examples

The val() method is an inbuilt method in jQuery which is used to returns or set the value of attribute for the selected elements. This method apply on the HTML form elements.

Syntax: There are three ways to use this method which are given below:

  • $(selector).val()

    Parameters: This ethod does not accepts any parameters.

  • $(selector).val(value)

    Parameters: This method accepts single parameter value which is mandatory. It is used to specify the value of attributes.

  • $(selector).val(function(index,current_value))

    Parameters: This method accepts single parameter function which is optional. It contains index position of element and current value attribute of selected element.

Return Value: This method returns the selected element with specified changes made by val() method.



Below examples illustrates the val() method in jQuery:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The val Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("button").click(function() {
                    $("input:text").val("GeeksforGeeks!");
                    $("input:text").css("color", "green");
                    $("input:text").css("font-size", "40px");
                    $("input:text").css("font-weight", "bold");
                    $("input:text").css("font-family", "times new roman");
                });
            });
        </script>
        <style>
            div {
                background-color: lightgreen;
                padding: 20px;
                width: 41%;
                min-height: 150px;
                border: 2px solid green;
            }
               
            input {
                border: 2px solid green;
                padding-left: 15px;
                width: 350px;
                height: 80px;
            }
        </style>
    </head>
    <body>
        <div>
            <p>
                <input type="text" name="user">
            </p>
            <!-- click on this paragraph and see the change -->
            <button>Click Here!</button>
        </div>
    </body>
</html>

chevron_right


Output:
val method

Example 2: This method takes two value first one for index position and second one for value of the attribute. No need to pass the index position, the function will automatically set to null (n = 0) and “c” specifies the current value of the input field.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>The val Method</title>
        <script src=
        </script>
          
        <!-- jQuery code to show the working of this method -->
        <script>
            $(document).ready(function() {
                $("button").click(function() {
                    $("input:text").val(function(n, c) {
                        return c + " GeeksforGeeks!";
                    });
                });
            });
        </script>
        <style>
            div {
                background-color: lightgreen;
                padding: 20px;
                width: 300px;
                height: 80px;
                border: 2px solid green;
            }
               
            input {
                border: 2px solid green;
                padding-left: 15px;
                width: 220px;
                font-weight: bold;
                height: 30px;
            }
        </style>
    </head>
       
    <body>
        <div>
            <p>
                <input type="text" name="user" value="Welcome To">
            </p>
            <!-- click on this paragraph and see the change -->
            <button>Click Here!</button>
        </div>
    </body>
</html>

chevron_right


Output:
Before click on the button:

After click on the button:



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.