jQuery html() with callback function

Description

jQuery html() 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>//w ww  .j  a  va2s . co m
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").html(function(n){
      return "This p element has index: " + n;
    });
  });
});
</script>
</head>
<body>

<button>Change the content of the p elements</button>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>



PreviousNext

Related