0

So i have a load of text and within the text is words that have brackets around them.

See below: enter image description here

At the moment i am doing a pre_replace in PHP to essentially put any text that are in between the [] into a span, where they are then coloured and given a class of clickable.

i essentially want to be able to click on the text and it copies that word to the clipboard.

At the moment i have this PHP:

$note = preg_replace('%(\[(.+?)\])%m', '<span style="color: #009EC0;" class="hover-cursor clickable">$1</span>', $note);

I found this jQuery online and just kind of changed the class names to mine:

// Copy replace variable to clipboard

document.getElementById('.clickable').addEventListener('click', function(event) 
{
    var copyTextarea = document.querySelector('.clickable');

    copyTextarea.select();

    try 
    {
        var successful = document.execCommand('copy');
        var msg = successful ? 'successful' : 'unsuccessful';
        console.log('Copying text command was ' + msg);
    } 
    catch (err) 
    {
        console.log('Oops, unable to copy');
    }
});

However this gives me an error saying: Cannot read property 'addEventListener' of null

So how can i achieve this?

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
Luke Litherland
  • 227
  • 3
  • 14

0 Answers0