Might be a pretty easy question, but I can't seem to solve it somehow.
I want to be able to select text inside a td-element from a table. The problem is there is a tab inside the text that i want to get rid of.
I tried this so far:
function getSelectionText(){
var selectedText = ""
if (window.getSelection){ // all modern browsers and IE9+
selectedText = window.getSelection().toString().trim()
}
return selectedText
}
$('td').click(function(){
var selected = getSelectionText();
document.execCommand('copy');
});
sadly this still returns the copied text with a tab behind it. This is really annoying when trying to copy passwords into a passwordfield, because the user can't see that there is a tab in the dotted password.
hopefully someone can help, thanks in advance!
EDIT:
I slightly changed the accepted answer and don't need the getSelectionText() function anymore. Hope this helps someone with the same problem.
$('td').click(function(){
var selected = $(this).html();
var inp = $("<input/>",{id:"copySelected"});
$("body").append(inp)
inp.val(selected);
inp[0].select();
document.execCommand('copy');
inp.remove();
});
Every click will copy the clicked td-cell to the clipboard