كيفية عمل زر copy to clipboard

ma2500

السلام عليكم اريد انشاء زر عند الضغط عليه يتم نسخ الكوبون الموجود

كيف ذلك جربت هذا الكود ولم ينجح

لا ادري ما المشكلة هل هناك حل بارك الله فيكم العمل بالووردبريس اخذت شفرة html بالمنتور ووضعت الكود

<!-- The text field -->

<input type="text" value="seev" id="myInput">

<!-- The button used to copy the text -->

<button onclick="myFunction()">انسخ الكود</button>

<script>

function myFunction() {

// Get the text field

var copyText = document.getElementById("myInput");

// Select the text field

copyText.select();

copyText.setSelectionRange(0, 99999); // For mobile devices

// Copy the text inside the text field

navigator.clipboard.writeText(copyText.value);

// Alert the copied text

alert("Copied the text: " + copyText.value);

}

</script>


الكود الصحيح هو كالتالي ويعمل على الهواتف أيضًا:

<input type="text" value="seev" id="myInput">
<button class="copy-button" onclick="myFunction()">انسخ الكود</button>
<script>
function myFunction() {
// Get the text field
var copyText = document.getElementById("myInput");
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.value);
// Alert the copied text
alert("Copied the text: " + copyText.value);
// Add a class to the button
document.querySelector(".copy-button").classList.add("active");
}
</script>

جزاك الله خير استاذي الكريم وبارك فيك

جربتها على الهاتف لم تعمل عندي لكن تعمل على اللابتوب فقط

الكود يعمل بالفعل على الهاتف:

جزاك الله خير في احد المتصفحات لم يعمل لكن الان عمل بشكل جيد بارك الله فيك