I would like to highlight the text within a tooltip when the user clicks the Short Url anchor so he can copy paste it. The tooltip is served by Twitter Bootstrap and the markup looks like this:
<div class="shorturl">
<a href="#" rel="tooltip_r" data-original-title="http://tmblr.co/ZPPojuQzc9bb">Short URI</a>
</div>
I found this snippet which I think would work just right except that I have not yet figured out how to handle the clicking of the link (which both does not scroll and highlights the text within the tooltip).
function selectText() {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementByClass('tooltip'));
range.select();
}
else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementByClass('tooltip'));
window.getSelection().addRange(range);
}
}
How can I make this work? Input very much appreciated!
This is what I would suggest to you : Live demo (jsfiddle)
And you might use some styles to improve the input in the tooltip. For example :
Update
If you need the same feature in dynamically loaded content, delegated events need to be used. Here is a working jsfiddle.