I'd like to be able to click on a point and have the contents of the tooltip for that point copied into the browser clipboard. I have a compromised solution here that copies the html of the tooltip into an input field and then copies it into the clipboard. I would ideally like to do this without the need for an input field, since this also requires focus to be moved to the input field.
I'm thinking that it should be possible to select the text of the tooltip itself and copy it from there rather than using the input as an intermediate.
In this example clicking on a point copies the html of the tooltip
$(function() {
$('#container').highcharts({
chart: {},
tooltip: {
useHTML: true,
borderWidth: 0,
style: {
padding: 0
}
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
events: {
click: function(event) {
copyToClipboard(this.chart.tooltip.label.div.innerHTML);
}
}
}]
});
// modified from another stackoverflow question
function copyToClipboard(html) {
// create hidden text element, if it doesn't already exist
var targetId = "clipboardInput";
target = document.getElementById("clipboardInput");
target.value = html;
target.focus();
target.setSelectionRange(0, target.value.length);
document.execCommand("cut");
}
});