jQuery - val( val ) Method



The val( val ) method sets the input value of every matched element.

If this method is called on radio buttons, checkboxes, or select options then it would checks, or selects them at the passed value.


Here is the simple syntax to use this method −

selector.val( val ) 


Here is the description of all the parameters used by this method −

  • val − If it is called on <input> but if it is called on <select> with the passed <option> value then passed option would be selected, if it is called on check box or radio box then all the matching check box and radiobox would be checked.


Following example would set the value attribute of the second input with the value content of the first input −

Live Demo
      <title>The Selecter Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            var content = $("input").val();
            $("input").val( content );
      <input type = "text" value = "First Input Box"/><br/>
      <input type = "text" value = "Second Input Box"/>

This will produce following result −