I want to copy text o span in clipboard, then show tooltip for it.
I use this code.
spans = document.querySelectorAll(".copy");
for (const span of spans) {
span.onclick = function() {
document.execCommand("copy");
}
span.addEventListener("copy", function (event) {
event.preventDefault();
if (event.clipboardData) {
event.clipboardData.setData("text/plain", span.textContent);
span.tooltip({ content: span.textContent });
span.tooltip("open");
}
});
}
it copy text to clipboard but dont show tooltip!!
2
Answers
I created a code snippet to test your code.
When I run the code snippet, and click on the "Hello!" text, I get this error:
The tooltip function is not a standard JavaScript function. Is it a jQuery function?
Since you have included the jQuery tag in your question, I assume that tooltip is a jQuery function, or a function from a jQuery plugin.
In this case you have to:
If
tooltip
is indeed a jQuery function, then you can use these lines of code:Assuming you are using jQuery UI, you need to include the jQuery and jQuery UI libraries in your HTML file before your JavaScript code. Then, you can initialize the tooltip on your span element using the tooltip() function.
you need to replace the span.tooltip({ content: span.textContent }); with $(span).attr("title", span.textContent); to set the title attribute of the span element to the tooltip text.