How To Get And Set Input Field Value in JQuery

How to Set value in input field?
To Set the value of the input field, We need to use JQuery val() Method.

$("button").click(function(){ 
 $("input:text").val("Bangla Tutorials Point"); 
});
What is val() and it's Usage?
The val() method sets the value for the selected elements.

This val() method sets the value of the value for all matched elements.

Mostly Used:
The val() method is mostly used with HTML

Different Syntax of val() Method:
val() Method for return the value:
$(selector).val();
val() Method for Set the Value:
$(selector).val(value);
val() Method for Set the value using function:
$(selector).val(function(index,currentvalue));
Required Field:
Here, Value is required. It specifies the of the value attribute
and function(index,currentvalue) is optional. It declear a function that returns a set of value.

Full Project Source Code:
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Title of the document</title> 
</head> 
<body> 
<input type="text" name="total" class="form-control" placeholder="Total" id="total"> 
<br> 
<input type="text" name="subtotal" class="form-control" id="subtotal" readonly> 
</body> 
</html>

//Script should be like this 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
 $('#total').change(function() { 
  var dInput = this.value; 
  $('#subtotal').val(dInput); 
  }); 
 }); 
</script>
In the following video, I have described with application about Set value in the input field and Get the value from input field. You can watch the video also to get better practical knowledge abount set value in the input field and Get the value from input field.



Thank you for visiting our blog-site. For more videos please visit our Cahnnel https://www.youtube.com/channel/UCW_xg9-8FU8SINc-EjDQ53g


EmoticonEmoticon