How do I edit the selected text of a textarea form element?
EDIT: as in edit it in-place, replacing the orignal text.
How do I edit the selected text of a textarea form element?
EDIT: as in edit it in-place, replacing the orignal text.
As a starting point, you can use this to get the selected text on the page:
function getSelectedText(){
if(window.getSelection){
return window.getSelection().toString();
}
else if(document.getSelection){
return document.getSelection();
}
else if(document.selection){
return document.selection.createRange().text;
}
}
This works:
function replaceIt(txtarea, newtxt) {
$(txtarea).val(
$(txtarea).val().substring(0, txtarea.selectionStart)+
newtxt+
$(txtarea).val().substring(txtarea.selectionEnd)
);
}
replaceIt($('textarea')[0], 'fun')