Insert content at the beginning of each p element
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//from w w w. ja v a 2s.co m <script> $(document).ready(function(){ $("button").click(function(){ $("p").prepend(function(n){ return "<b>This p element has index " + n + "</b>. "; }); }); }); </script> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>test</button> </body> </html>