jQuery val() with callback function

Description

jQuery val() with callback function

View in separate window

<!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>



PreviousNext

Related