jQuery val Method


jQuery val method : is used to get and set value in form fields.


Syntax of jQuery val() method

Get Value

$(selector).val();

Note : It does not accepts any argument.
It is used to get form field value.

jQuery val Method Example 1

jQuery val() Method – Example 1 – Get The val

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#valExample" ).on( "click", function(event) {             
                 var data = $("#fname").val();
                 alert(data);
              });
               
          });
</script>

Try it »

Set Val

$(selector).val(value);

value:value to be set.

jQuery val method Example 2

jQuery val() Method – Example 2 – Set The val/h3>

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#valExample" ).on( "click", function(event) {             
                 $("fname").val("John Doe");
              });
               
          });
</script>

Try it »


Advertisements

Add Comment

📖 Read More