Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How could we copy a component's text to the clipboard with HTMX? #531

Open
cbmeeks opened this issue Apr 2, 2024 · 2 comments
Open

How could we copy a component's text to the clipboard with HTMX? #531

cbmeeks opened this issue Apr 2, 2024 · 2 comments

Comments

@cbmeeks
Copy link

cbmeeks commented Apr 2, 2024

I have two large text fields and it would be really cool to have an HTMX button to copy the contents to the clipboard.
I can do this in vanilla JS but was wondering if there is some clever way to do this using inline HTMX.

Any suggestions?

Thanks.

@andryyy
Copy link

andryyy commented Apr 3, 2024

Since you were asking for HTMX:

<span hx-on:click="!window.s?s=this.textContent:null;navigator.clipboard.writeText(s);this.textContent='Copied';setTimeout(()=>{this.textContent=s}, 1000)">WriteMeBabyOneMoreTime</span>

This makes sure not to copy "Copied" to the clipboard when the text was replaced.

@devalexwhite
Copy link

devalexwhite commented Apr 11, 2024

Just in case you were wondering how to do this in hyperscript (as this is the hyperscript repo), you can see an example on the hyperscript homepage with the CDN link.

<div>
  <code style="border: 2px dotted #00000055; margin: 0 10px; padding: 4px 6px; border-radius: 4px">&lt;script src="https://unpkg.com/hyperscript.org@0.9.12"&gt;&lt;/script&gt;</code>
  
  <button style="font:inherit;font-size:.8em;background:#3465a4;color:white;border:none;padding: 0 .4em; border-radius: .4em" _="on click
    writeText(my previousElementSibling's innerText) on navigator.clipboard
    put 'copied!' into me
    wait 1s
    put 'copy' into me">copy</button>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants