-1

I need a copy-to-text button using a font awesome icon in a div in wordpress.

I do not want any alerts. Just a simple click.

<div class="btcTXT">text</div>

<div id="cpy" class="cpy"><i class="far fa-copy"></i></div>

I am a novice so please tell me the steps I need to do.

Thank you in advance.

2 Answers 2

0

Give your div an id first, like so:

<div class="btcTXT" id="your-div-id">text</div>

Then write this javascript to do the trick for you!

document.getElementById("cpy").addEventListener("click", () => {
    navigator.clipboard.writeText(document.getElementById("your-div-id").textContent).then(() => {
        console.log('Copied to clipboard!!!');
    });
});

Make sure you "enqueue/inject" your javascript to the page you're testing.

Let me know if you were able to get it to work!

Sign up to request clarification or add additional context in comments.

12 Comments

How do I place javascript inside a wordpress page? Its doesn't seem to work if I add it to the page.
I don't know what you mean with "enqueue/inject"
I can't seen to figure out how to place the javascript.
In wordpress you need to load your javascript onto the page. Do you know how to do it?
I just tested it on my wordpress website and it works seamlessly fine.
|
0

What @Ruvee said is correct. You typically need to enqueue scripts on your site to make sure everything works as expected. If that isn't your cup of tea I would suggest using a plugin like Simple CSS and JS Plugin.

You can add that piece of code as a new JS file using that plugin and should work just fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.