Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

freemanx's avatar

Create a copy button in laravel

Hi,i want to create a copy button in my laravel project but it doesn't work.When i pasted it's show my blade code

{{ config('app.url') }}/exam/{{ $candidat->short }}

0 likes
5 replies
Tray2's avatar

What is it you want to copy and what does your code look like?

freemanx's avatar

I wrote the code in javascript, the button is supposed to copy a link on the clipboard

textarea id="to-copy" class="">{{ config('app.url' . '/exam/' . $candidat->short) }}/textarea

button id="copy" type="button" class="btn btn-success btn-sm ">Copier le lien</button

this is my JS CODE ,i have some problem with markdown preview

var toCopy = document.getElementById('to-copy'), btnCopy = document.getElementById('copy'), paste = document.getElementById('cleared'); btnCopy.addEventListener('click', function() { toCopy.select(); paste.value = ''; if (document.execCommand('copy')) { btnCopy.classList.add('copied'); paste.focus(); var temp = setInterval(function() { btnCopy.classList .remove('copied'); clearInterval(temp); }, 600); } else { console.info( 'document.execCommand went wrong…' ) } return false; });
Tray2's avatar

wrap you code with three backtics to make it readable. Trying to read that gooblygook is like reading a minified css or js file.

Code here
freemanx's avatar
freemanx
OP
Best Answer
Level 1

<p> Copiez cette adresse : <span id="tocopy">{{ config('app.url') }}/test_de_connaissance/{{ $candidat->short }}</span> <button type="button" class="js-copy btn btn-success" data-target="#tocopy">copier</button> </p>

var btncopy = document.querySelector('.js-copy'); if (btncopy) { btncopy.addEventListener('click', docopy); } function docopy() { var range = document.createRange(); var target = this.dataset.target; var fromElement = document.querySelector(target); var selection = window.getSelection(); range.selectNode(fromElement); selection.removeAllRanges(); selection.addRange(range); try { var result = document.execCommand('copy'); if (result) { La copie a réussi alert('Copié !');} } catch (err) { Une erreur est surevnue lors de la tentative de copie alert(err); } selection = window.getSelection(); if (typeof selection.removeRange ==='function') { selection.removeRange(range); } else if (typeof selection.removeAllRanges === 'function') { selection.removeAllRanges(); } }

Please or to participate in this conversation.