Issue
I want to copy input of a specific tag without having to make an input field to my clipboard using JavaScript
JavaScript and HTML
function copy(input){
}
<p>Text To Copy = hi <button type="button" onclick="copy('hi')">click to copy</button></p>
Solution
function copy_text_fun() {
//getting text from P tag
var copyText = document.getElementById("copy_txt");
// creating textarea of html
var input = document.createElement("textarea");
//adding p tag text to textarea
input.value = copyText.textContent;
document.body.appendChild(input);
input.select();
document.execCommand("Copy");
// removing textarea after copy
input.remove();
alert(input.value);
}
<p id="copy_txt">hi</p>
<button onclick="copy_text_fun()">Copy</button>
here is with the paragraph tag,
Answered By - CodeBug
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.