jQuery val()
with callback function
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>// ww w .ja va 2 s .c om <script> $(document).ready(function(){ $("button").click(function(){ $("input:text").val(function(n, c){ return c + " new value"; }); }); }); </script> </head> <body> <p>Name: <input type="text" name="user" value="Peter"></p> <button>Set the value of the input field</button> </body> </html>