Introduction:

In this article we will explain how to set or get value in a html input control using jquery or What is val() method in jquery or why use val () method in jquery with example.

Description:

The jQuery val() method is used to set the value or get the value of the selected input elements.  

Set value: When you use this method to set the value of all matched input elements.

Get value: When you use this method to get the value of the first matched input elements.

Let’s see simple Example:

jQuery  val () Method (set value)

 

In this example we set the value of input (textbox) elements.

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Example of jQuery </title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('input').val('Aditya Kumar');
        });
    </script>
</head>
<body>
    <div>
        <input type="text" value="" />        
     </div>    
</body>
</html>

OutPut:



jQuery  val () Method (get value)

 

In this example we get the value of textbox and set the value  in textarea input element.

 

 

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Example of jQuery </title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('textarea').val($('input').val());
        });
    </script>
</head>
<body>
    <div>
        <input type="text" value="Aditya Kumar" /> <br /><br /> 
         <textarea id="TextArea1" rows="2" cols="20"></textarea>   
     </div>    
</body>
</html>

 

OutPut:




I hope this article will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like