Home >>jQuery Tutorial >jQuery val() Method
jQuery val() method in jQuery is used to returns the value attribute of the FIRST matched selected elements.
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 | 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:
|
<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>
Enter Your Name: