Here’s a complete, runnable HTML + JavaScript example that copies the entire page’s HTML source to the clipboard when you click a button — excluding the button itself from the copied content.
We achieve this by cloning the document body, removing the button, and then copying the remaining HTML.
Html
Copy Whole Page HTML
Example Page
This is some sample content that will be copied.
How it works: Clone the body: We duplicate the page’s so we can safely remove elements without affecting the live page. Remove the button: The querySelector("#copyBtn") finds and removes the copy button from the cloned content. Get HTML: clonedBody.innerHTML retrieves the HTML string. Copy to clipboard: navigator.clipboard.writeText() copies the HTML to the clipboard. Error handling: Alerts and console logs handle any failures. 💡 Tip: If you want to copy the entire HTML document (including , , and tags), you can replace: Jslet htmlContent = clonedBody.innerHTML; with: Jslet htmlContent = document.documentElement.outerHTML; But then you’ll need to remove the button from the DOM before copying. Do you want me to give you the version that copies the full HTML document including but still excludes the button? That would be more complete for source backup purposes.