I have a "copy to clipboard" button that copies text generated from a search query that is going to be pasted in the url. However, when I go to paste it, the <br>
tags are visible in the string. I understand how to use the .replace() function, but what I am wondering is if it is possible to save the words that are copied using the document.execCommand("copy")?
Here is my code JavaScript:
function copyToClipboard(elementId) {
// Create a "hidden" input
var aux = document.createElement("input");
// Assign it the value of the specified element
aux.setAttribute("value", document.getElementById(elementId).innerHTML);
// Append it to the body
document.body.appendChild(aux);
// Highlight its content
aux.select();
// Copy the highlighted text
document.execCommand("copy");
// Remove it from the body
document.body.removeChild(aux);
//var query = aux.replace("<br>", "");
console.log(aux);
//document.body.replace(aux, queryItem);
}
And here is my HTML:
<div id="QueryStringContainer" class="QueryStringContainer">
<div id="QueryString" class="control-label">this is a query string for users to consume</div>
<button onclick="copyToClipboard('QueryString')">Copy to Clipboard</button>
</div>
Say the form generates the string The<br>cat<br>ran<br>fast
,
From what I have, how can I save the string as a variable using that function?
Thank you for the help!
Edit:
In the console, it prints <input value="The<br>cat<br>ran<br>fast">