Home >>jQuery Tutorial >jQuery val() Method

jQuery val() Method

jQuery val() Method

jQuery val() method in jQuery is used to returns the value attribute of the FIRST matched selected elements.

Syntax:

Return the value attribute: $(selector).val()
Set the value attribute: $(selector).val(value)
Set the value attribute using a function: $(selector).val(function(index,currentvalue))

Parameter Values

Parameter Description
value It is a required parameter and is used to Specifies the value attribute
function(index,currentvalue) It is optional parameter and is used to returns the value to set specific function.

Possible values:

  • index – It is used to returns the index position of the element
  • currentvalue – It is used to returns the current value attribute of selected elements
Here is an Example of jQuery val() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$("input:text").val("Welcome to Phptpoint");
});
});
</script>
</head>
<body>
<p>Enter Your Name: <input type="text" name="username"></p>
<button class="btn1">Click me to Set the value</button>
</body>
</html>

Output:

Enter Your Name:


No Sidebar ads