I have script that copy ID internal content to user clipboard (style CTRL+C)
IF the URL contain & and the script change it to &.
How can i prevent it? i would like it to still be ‘&’ and not &
Thanks
HTML
Link: <span id="faqURL"><?PHP echo 'http://'.SITE_ADDRESS_ROOT.'/index.php?page=search&search_type=faq&faqID='.$_GET['faqID']; ?></span>
<a onclick="copyToClipboard('faqURL')">Copy</a>
SCRIPT
function copyToClipboard(elementId) {
var aux = document.createElement("input");
aux.setAttribute("value", document.getElementById(elementId).innerHTML);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
}
Advertisement
Answer
You can’t, and don’t need to. & is how HTML represents a & character.
If you want an HTML representation of some data, then you need &.
If you don’t want an HTML representation of some data, then don’t use innerHTML in the first place. Use textContent to get a text representation instead.