How do I edit the selected text of a textarea form element?
EDIT: as in edit it in-place, replacing the orignal text.
This works:
function replaceIt(txtarea, newtxt) { $(txtarea).val( $(txtarea).val().substring(0, txtarea.selectionStart)+ newtxt+ $(txtarea).val().substring(txtarea.selectionEnd) ); } $("button").on('click', function() { replaceIt($('textarea')[0], 'fun') })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea>Hello world.</textarea> <button>Replace with fun</button>
1.4m articles
1.4m replys
5 comments
57.0k users