It is used to set the current value of a particular element in the set of matched elements.
it sets the value of every matched element.
The default value of input field is empty string so it is blank.If user types something in input field, then that value will be assigned to val attribute.
val method can be called on <input>, checkbox , radio button and <select>.
Syntax:
$(selector).val() // used to get value
$(selector).val(value) // used to set value
$(selector).val(function(index,currentvalue)) // used to set value using function.
Example:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("input:text").val("This is val method");
});
});
</script>
</head>
<body>
<p>Name: <input type="text"></p>
<button>Click button to get value</button>
</body>
</html>
/*When you will click this button, you will get output as "This is val method" */
0 Comment(s)