JavaScript inject 3 Paragraphs after H1 Heading tag

Sitebee

Technical SEO
Staff member
Code:
<script>
// JS script to dynamically inject 3 paragraphs after the H1 heading tag.
  (function() {
    // Create the new P element variables
    var para0 = document.createElement('p');
    var para1 = document.createElement('p');
    var para2 = document.createElement('p');
    
    // Add the text content
    para0.innerText = "This is the contents of the first paragraph. This is the contents of the first paragraph.";

    para1.innerText = "This is the contents of the second paragraph. This is the contents of the second paragraph.";

    para2.innerText = "This is the contents of the third paragraph. This is the contents of the third paragraph.";
    
    // Create the variable to inject after H1 element
    var title = document.querySelector('h1');
    
    // Inject the new P elements after the H1 element
    if (title) {
      title.parentElement.insertBefore(para0, title.nextSibling); }
    
    if (title) {
      title.parentElement.insertBefore(para1, title.nextSibling.nextSibling); }
    
    if (title) {
      title.parentElement.insertBefore(para2, title.nextSibling.nextSibling.nextSibling); }

  }) ();
</script>