# .val()

Get the current value of the first element in the set of matched elements or set the value of every matched element.

## val ( ) version added: 1.0

Description: Get the current value of the first element in the set of matched elements.

The `.val()` method is primarily used to get the values of form elements such as `input`, `select` and `textarea`. In the case of `select` elements, it returns `null` when no option is selected and an array containing the value of each selected option when there is at least one and it is possible to select more because the `multiple` attribute is present.

For selects and checkboxes, you can also use the :selected and :checked selectors to get at values, for example:

    // Get the value from a dropdown select
    $( "select.foo option:selected").val();

    // Get the value from a dropdown select even easier
    $( "select.foo" ).val();

    // Get the value from a checked checkbox
    $( "input:checkbox:checked" ).val();

    // Get the value from a set of radio buttons
    $( "input:radio[name=bar]:checked" ).val();

Note: At present, using `.val()` on textarea elements strips carriage return characters from the browser-reported value. When this value is sent to the server via XHR however, carriage returns are preserved (or added by browsers which do not include them in the raw value). A workaround for this issue can be achieved using a valHook as follows:

    $.valHooks.textarea = {
      get: function( elem ) {
        return elem.value.replace( /\r?\n/g, "\r\n" );
      }
    };

## Examples:

#### Example: Get the single value from a single select and an array of values from a multiple select and display their values.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>css demo</title>

      <style>

      p {
        color: red;
        margin: 4px;
      }
      b {
        color: blue;
      }

      </style>

      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

    <p></p>

    <select id="single">
      <option>Single</option>
      <option>Single2</option>
    </select>

    <select id="multiple" multiple="multiple">
      <option selected="selected">Multiple</option>
      <option>Multiple2</option>
      <option selected="selected">Multiple3</option>
    </select>

    <script>

    function displayVals() {
      var singleValues = $( "#single" ).val();
      var multipleValues = $( "#multiple" ).val() || [];
      $( "p" ).html( "<b>Single:</b> " + singleValues +
        " <b>Multiple:</b> " + multipleValues.join( ", " ) );
    }

    $( "select" ).change( displayVals );
    displayVals();

    </script>

    </body>
    </html>

#### Demo:

#### Example: Find the value of an input box.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>css demo</title>

      <style>

      p {
        color: blue;
        margin: 8px;
      }

      </style>

      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

    <input type="text" value="some text">
    <p></p>

    <script>

    $( "input" )
      .keyup(function() {
        var value = $( this ).val();
        $( "p" ).text( value );
      })
      .keyup();

    </script>

    </body>
    </html>

#### Demo:

## val ( value ) Returns: jQuery version added: 1.0

- value
  Type:
  A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked.

## val ( function(index, value) ) Returns: jQuery version added: 1.4

- function(index, value)
  Type: Function
  A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments.

Description: Set the value of each element in the set of matched elements.

This method is typically used to set the values of form fields.

Passing an array of element values allows matching `<input type="checkbox">`, `<input type="radio">` and `<option>`s inside of n `<select multiple="multiple">` to be selected. In the case of `<input type="radio">`s that are part of a radio group and `<select multiple="multiple">` the other elements will be deselected.

The `.val()` method allows us to set the value by passing in a function. As of jQuery 1.4, the function is passed two arguments, the current element's index and its current value:

    $( "input:text.items" ).val(function( index, value ) {
      return value + " " + this.className;
    });

This example appends the string " items" to the text inputs' values.

## Examples:

#### Example: Set the value of an input box.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>css demo</title>

      <style>

      button {
        margin: 4px;
        cursor: pointer;
      }
      input {
        margin: 4px;
        color: blue;
      }

      </style>

      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

    <div>
      <button>Feed</button>
      <button>the</button>
      <button>Input</button>
    </div>
    <input type="text" value="click a button">

    <script>

    $( "button" ).click(function() {
      var text = $( this ).text();
      $( "input" ).val( text );
    });

    </script>

    </body>
    </html>

#### Demo:

#### Example: Use the function argument to modify the value of an input box.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>css demo</title>

      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

    <p>Type something and then click or tab out of the input.</p>
    <input type="text" value="type something">

    <script>

    $( "input" ).on( "blur", function() {
      $( this ).val(function( i, val ) {
        return val.toUpperCase();
      });
    });

    </script>

    </body>
    </html>

#### Demo:

#### Example: Set a single select, a multiple select, checkboxes and a radio button .

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>css demo</title>

      <style>

      body {
        color: blue;
      }

      </style>

      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

    <select id="single">
      <option>Single</option>
      <option>Single2</option>
    </select>

    <select id="multiple" multiple="multiple">
      <option selected="selected">Multiple</option>
      <option>Multiple2</option>
      <option selected="selected">Multiple3</option>
    </select>

    <br>
    <input type="checkbox" name="checkboxname" value="check1"> check1
    <input type="checkbox" name="checkboxname" value="check2"> check2
    <input type="radio" name="r" value="radio1"> radio1
    <input type="radio" name="r" value="radio2"> radio2

    <script>

    $( "#single" ).val( "Single2" );
    $( "#multiple" ).val([ "Multiple2", "Multiple3" ]);
    $( "input").val([ "check1", "check2", "radio1" ]);

    </script>

    </body>
    </html>

#### Demo: