13 September 2024

To append content to an HTML element using JavaScript, you can use appendChild() for DOM elements or innerHTML for strings.

Source code viewer
  1. // Get the element you want to append to
  2. let container = document.getElementById('container');
  3.  
  4. // Create a new element
  5. let newElement = document.createElement('p');
  6.  
  7. // Add some content to the new element
  8. newElement.textContent = 'This is a new paragraph.';
  9.  
  10. // Append the new element to the container
  11. container.appendChild(newElement);
  12.  
  13. // OR
  14.  
  15. container.innerHTML += '<p>This is another new paragraph.</p>';
Programming Language: Javascript