function getSelection() { var text = ""; if (window.getSelection) { text = window.getSelection().toString(); } return text; } document.onselectionchange = function() { document.getElementById("sel").value = getSelectionText(); };