<div class="discount-code-container" style="margin: 20px 0; padding: 15px; background:…
- User
- Assistant
- User
- Assistant
Your discount code block and copy functionality look great! Just a small suggestion to improve the JavaScript function for better compatibility and clarity:
Here is a slightly refined version of your script that incorporates these improvements:
<div class="discount-code-container" style="margin: 20px 0; padding: 15px; background: #f8f8f8; border-radius: 5px; text-align: center;">
<h3 style="margin-top: 0;">Use Discount Code & Save!</h3>
<div class="discount-code" style="display: flex; justify-content: center; align-items: center;">
<input type="text" id="discountCode" value="INSTA5" readonly style="padding: 8px; border: 1px solid #ddd; border-radius: 4px 0 0 4px; width: 100px; text-align: center; font-weight: bold;">
<button onclick="copyDiscountCode(event)" style="padding: 8px 12px; background: #4CAF50; color: white; border: none; border-radius: 0 4px 4px 0; cursor: pointer;">
Copy
</button>
</div>
<p style="margin-bottom: 0; font-size: 0.9em;">Apply this code at checkout to get $5 off your order</p>
</div>
<script>
function copyDiscountCode(event) {
const codeInput = document.getElementById('discountCode');
const button = event.target;
const originalText = button.textContent;
if (navigator.clipboard && window.isSecureContext) {
// Use Clipboard API if available
navigator.clipboard.writeText(codeInput.value).then(() => {
showCopiedState(button, originalText);
}, () => {
fallbackCopy(codeInput, button, originalText);
});
} else {
// Fallback to older execCommand method
fallbackCopy(codeInput, button, originalText);
}
}
function fallbackCopy(inputElement, button, originalText) {
inputElement.select();
inputElement.setSelectionRange(0, inputElement.value.length);
try {
const successful = document.execCommand('copy');
if(successful) {
showCopiedState(button, originalText);
} else {
alert('Failed to copy code. Please copy manually.');
}
} catch (err) {
alert('Failed to copy code. Please copy manually.');
}
window.getSelection().removeAllRanges(); // Clear selection
}
function showCopiedState(button, originalText) {
button.textContent = 'Copied!';
button.style.backgroundColor = '#45a049';
setTimeout(() => {
button.textContent = originalText;
button.style.backgroundColor = '#4CAF50';
}, 3000);
}
</script>
What changed?
onclick handler now passes the event object.execCommand if needed.This will provide a more robust copy experience, especially on modern browsers. Let me know if you want me to adapt it further!
Your updated code looks clean, modern, and should work well across browsers supporting the Clipboard API and those that don't.
Here are a few optional improvements you might consider for a better user experience and accessibility:
aria-live region for screen readers to announce the copy statusThis can help visually impaired users know when the code is copied.
Add this below your discount code container:
<div id="copyStatus" aria-live="polite" style="position: absolute; left: -9999px; height: 1px; width: 1px; overflow: hidden;"></div>
And update your showCopiedState function like this:
function showCopiedState(button, originalText) {
button.textContent = 'Copied!';
button.style.backgroundColor = '#45a049';
// Announce status for screen readers
const status = document.getElementById('copyStatus');
if (status) {
status.textContent = 'Discount code copied to clipboard';
}
setTimeout(() => {
button.textContent = originalText;
button.style.backgroundColor = '#4CAF50';
if (status) {
status.textContent = ''; // Clear announcement
}
}, 3000);
}
select() on readonly input since on some mobile browsers it doesn’t select properly. Using navigator.clipboard handles it well, and the fallback is mostly for desktop.If that fallback rarely triggers on mobile, it should be fine as-is.
aria-live update:<div class="discount-code-container" style="margin: 20px 0; padding: 15px; background: #f8f8f8; border-radius: 5px; text-align: center;">
<h3 style="margin-top: 0;">Use Discount Code & Save!</h3>
<div class="discount-code" style="display: flex; justify-content: center; align-items: center;">
<input type="text" id="discountCode" value="INSTA5" readonly style="padding: 8px; border: 1px solid #ddd; border-radius: 4px 0 0 4px; width: 100px; text-align: center; font-weight: bold;">
<button onclick="copyDiscountCode(event)" style="padding: 8px 12px; background: #4CAF50; color: white; border: none; border-radius: 0 4px 4px 0; cursor: pointer;">
Copy
</button>
</div>
<p style="margin-bottom: 0; font-size: 0.9em;">Apply this code at checkout to get $5 off your order</p>
</div>
<!-- Aria-live region for screen reader announcements -->
<div id="copyStatus" aria-live="polite" style="position: absolute; left: -9999px; height: 1px; width: 1px; overflow: hidden;"></div>
<script>
function copyDiscountCode(event) {
const codeInput = document.getElementById('discountCode');
const button = event.target;
const originalText = button.textContent;
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(codeInput.value).then(() => {
showCopiedState(button, originalText);
}, () => {
fallbackCopy(codeInput, button, originalText);
});
} else {
fallbackCopy(codeInput, button, originalText);
}
}
function fallbackCopy(inputElement, button, originalText) {
inputElement.select();
inputElement.setSelectionRange(0, inputElement.value.length);
try {
const successful = document.execCommand('copy');
if(successful) {
showCopiedState(button, originalText);
} else {
alert('Failed to copy code. Please copy manually.');
}
} catch (err) {
alert('Failed to copy code. Please copy manually.');
}
window.getSelection().removeAllRanges();
}
function showCopiedState(button, originalText) {
button.textContent = 'Copied!';
button.style.backgroundColor = '#45a049';
const status = document.getElementById('copyStatus');
if (status) {
status.textContent = 'Discount code copied to clipboard';
}
setTimeout(() => {
button.textContent = originalText;
button.style.backgroundColor = '#4CAF50';
if (status) {
status.textContent = '';
}
}, 3000);
}
</script>
If you want me to help with anything else—styling, responsive tweaks, different copy button styles—just let me know!
Not yet included in a published release.