javascript - Copy selected text from one textarea to another -
i have 2 <textarea>s. 1 id="input" , other id="selection".
<textarea id="input"> contain html. user select text in textarea, click button , selected text copied <textarea id="selection">.
i can use jquery or vanilla javascript , i'd work in ie7+, safari , firefox.
the following it:
see in action: http://www.jsfiddle.net/qenbv/1/
function getselectedtext(el) { if (typeof el.selectionstart == "number") { return el.value.slice(el.selectionstart, el.selectionend); } else if (typeof document.selection != "undefined") { var range = document.selection.createrange(); if (range.parentelement() == el) { return range.text; } } return ""; } function copyselected() { var srctextarea = document.getelementbyid("input"); var desttextarea = document.getelementbyid("selection"); desttextarea.value = getselectedtext(srctextarea); } <input type="button" onclick="copyselected()" value="copy selected">
Comments
Post a Comment