jQuery val()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery val()

There are two usage of jQuery val() method.

  • It is used to get current value of the first element in the set of matched elements.
  • It is used to set the value of every matched element.

Syntax:

  1. $(selector).val()  

It is used to get value.

  1. $(selector).val(value)  

It is used to set value.

  1. $(selector).val(function(index,currentvalue))   

It is used to set value using function.

Parameters of jQuery val() method

Parameter Description
Value It is a mandatory parameter. It is used specify the value of the attribute.
Function (index, currentvalue) It is an optional parameter. It is used to specify a function that returns the value to set.

jQuery val() example

The val() method is primarily used to get the values of form elements. This method doesn't accept any arguments. This method returns a NULL when no option is selected and it returns an array containing the value of each selected options in the case of one or more selection.

Let's see the example of val() method.

  1. <!DOCTYPE html>    
  2. <html lang="en">    
  3. <head>    
  4.   <meta charset="utf-8">    
  5.   <title>val demo</title>    
  6.   <style>    
  7.   p {    
  8.     color: red;    
  9.     margin: 4px;    
  10.   }    
  11.   b {    
  12.     color: blue;    
  13.   }    
  14.   </style>    
  15.   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>    
  16. </head>    
  17. <body>    
  18. <p></p>    
  19. <select id="single">    
  20.   <option>Single</option>    
  21.   <option>Double</option>    
  22.  <option>Triple</option>    
  23. </select>    
  24. <script>    
  25. function displayVals() {    
  26.   var singleValues = $( "#single" ).val();    
  27.   $( "p" ).html( "<b>Value:</b> " + singleValues);    
  28. }    
  29. $( "select" ).change( displayVals );    
  30. displayVals();    
  31. </script>    
  32. </body>    
  33. </html>   

Test it Now

Output:

Value: Single

Let's see example of jQuery val() method with single and multiple select boxes.

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>val demo</title>  
  6.   <style>  
  7.   p {  
  8.     color: red;  
  9.     margin: 4px;  
  10.   }  
  11.   b {  
  12.     color: blue;  
  13.   }  
  14.   </style>  
  15.   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  16. </head>  
  17. <body>  
  18. <p></p>  
  19. <select id="single">  
  20.   <option>Single</option>  
  21.   <option>Single2</option>  
  22.   <option>Single3</option>  
  23. </select>  
  24. <select id="multiple" multiple="multiple">  
  25.   <option selected="selected">Multiple</option>  
  26.   <option>Multiple2</option>  
  27.   <option>Multiple3</option>  
  28. </select>  
  29. <script>  
  30. function displayVals() {  
  31.   var singleValues = $( "#single" ).val();  
  32.   var multipleValues = $( "#multiple" ).val() || [];  
  33.   $( "p" ).html( "<b>Single:</b> " + singleValues +  
  34.     " <b>Multiple:</b> " + multipleValues.join( ", " ) );  
  35. }  
  36. $( "select" ).change( displayVals );  
  37. displayVals();  
  38. </script>  
  39. </body>  
  40. </html>  

Test it Now

Output:

Single: Single Multiple: Multiple

 

jQuery val(value) example

This method is used to set a string of text, a number, an array of strings corresponding to the value of each matched element. This method facilitates you to set the value by passing in the function.

Let's see the example of val(value) method.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("button").click(function(){  
  8.         $("input:text").val("JavaTpoint");  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <p>Name: <input type="text" name="user"></p>  
  15. <button>Set the value of the input field</button>  
  16. </body>  
  17. </html>  

Test it Now

Output:

Name: 

Set the value of the input field