<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg" -->

---
title: A simple "copy this code" button in JavaScript | daily.dev
description: A walkthrough of building a lightweight copy-to-clipboard button for code samples using vanilla JavaScript, HTML, and CSS. Covers using...
canonical: https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: A simple "copy this code" button in JavaScript | daily.dev
og:description: A walkthrough of building a lightweight copy-to-clipboard button for code samples using vanilla JavaScript, HTML, and CSS. Covers using...
og:url: https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg
og:image: https://api.daily.dev/og/posts/1lsBtExWG.png
og:image:alt: A simple "copy this code" button in JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# A simple "copy this code" button in JavaScript

**[Terence Eden](https://daily.dev/sources/shkspr)** · 2 min read · 2 upvotes · 0 comments

## Summary

A walkthrough of building a lightweight copy-to-clipboard button for code samples using vanilla JavaScript, HTML, and CSS. Covers using navigator.clipboard.writeText tied to a click event, different ways to locate the associated code element (via parentNode traversal, itemprop, or nextSibling), and using the native HTML popover attribute plus a dialog element to show a temporary confirmation message without needing much JS. A short setTimeout call hides the popover after three seconds, and CSS handles positioning it near the button.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://shkspr.mobi/blog/2026/08/a-simple-copy-this-code-button-in-javascript>

## Questions this post answers

### How do I make a copy-to-clipboard button for code blocks using plain JavaScript?

Use navigator.clipboard.writeText inside a button's onclick handler, passing the plaintext content of the nearby code element, for example this.parentNode.getElementsByTagName('code')[0].textContent. The clipboard write requires a user interaction like a click to succeed, so tying it directly to the button's onclick satisfies that requirement without extra event listener setup.

_daily.dev surfaces practical vanilla JS snippets like this for developers polishing code-sharing UX._

### How can I show a temporary confirmation popup without writing custom JavaScript for show and hide logic?

Use the native HTML popover attribute on a dialog element combined with popovertarget and popovertargetaction=show on the trigger button, which shows the popup with zero JavaScript. Clicking outside dismisses it automatically; a small setTimeout calling hidePopover() after a set delay (such as 3000ms) auto-hides it if desired, and CSS controls its position.

_developers exploring native browser APIs can track features like popover here on daily.dev._

## Similar posts on daily.dev

- [Creating a Copy Code Button in Kendo UI for Vue](https://daily.dev/posts/creating-a-copy-code-button-in-kendo-ui-for-vue-kidtezt2b) · Telerik · 0 upvotes · 0 comments
- [Fix navigator clipboard writeText transient activation Issues](https://daily.dev/posts/fix-navigator-clipboard-writetext-transient-activation-issues-mrm9tubve) · SitePoint · 1 upvotes · 0 comments
- [Getting Started With The Popover API — Smashing Magazine](https://daily.dev/posts/getting-started-with-the-popover-api-smashing-magazine-vjbltzl0j) · Smashing Magazine · 47 upvotes · 1 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#css](https://daily.dev/tags/css), [#html](https://daily.dev/tags/html)

[View this post on daily.dev](https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"A simple \"copy this code\" button in JavaScript","url":"https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg"},"datePublished":"2026-08-29T11:39:19.528Z","dateModified":"2026-09-14T08:58:59.455Z","description":"A walkthrough of building a lightweight copy-to-clipboard button for code samples using vanilla JavaScript, HTML, and CSS. Covers using...","image":"https://media.daily.dev/image/upload/s--0_ODbtD2--/f_auto/v1722860399/public/Placeholder%2008","thumbnailUrl":"https://media.daily.dev/image/upload/s--0_ODbtD2--/f_auto/v1722860399/public/Placeholder%2008","isAccessibleForFree":true,"articleSection":"Terence Eden","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Terence Eden","logo":"https://media.daily.dev/image/upload/s--nhylgPuf--/f_auto,q_auto/v1774964413/logos/shkspr?_a=BAMAMiWQ0","url":"https://daily.dev/sources/shkspr"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,css,html","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Terence Eden","item":"https://daily.dev/sources/shkspr"},{"@type":"ListItem","position":3,"name":"A simple \"copy this code\" button in JavaScript"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg#faq","mainEntity":[{"@type":"Question","name":"How do I make a copy-to-clipboard button for code blocks using plain JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Use navigator.clipboard.writeText inside a button's onclick handler, passing the plaintext content of the nearby code element, for example this.parentNode.getElementsByTagName('code')[0].textContent. The clipboard write requires a user interaction like a click to succeed, so tying it directly to the button's onclick satisfies that requirement without extra event listener setup. daily.dev surfaces practical vanilla JS snippets like this for developers polishing code-sharing UX."}},{"@type":"Question","name":"How can I show a temporary confirmation popup without writing custom JavaScript for show and hide logic?","acceptedAnswer":{"@type":"Answer","text":"Use the native HTML popover attribute on a dialog element combined with popovertarget and popovertargetaction=show on the trigger button, which shows the popup with zero JavaScript. Clicking outside dismisses it automatically; a small setTimeout calling hidePopover() after a set delay (such as 3000ms) auto-hides it if desired, and CSS controls its position. developers exploring native browser APIs can track features like popover here on daily.dev."}}]}
```

