I have a textarea where when the user selects and presses enter I want it to change to something else. For example I have a textarea, when a user selects something like NY, I want those two letters to change to NY
I have something like :
$('textarea').html('I live in NY and it\'s a great place to live');
$(window).click(function(){
var selection = selectedText();
console.log(selection);
});
function selectedText() {
var ret = '';
if (window.getSelection){
ret = window.getSelection();
} else if (document.getSelection) {
ret = document.getSelection();
} else if (document.selection) {
ret = document.selection.createRange().text;
}
return ret;
}
I don’t know where to check to see if it’s from a textarea or from somewhere else, and I don’t know how to change a specific part of a textarea’s text
There are a lot of cross browser nuances when it comes to text selection. There are several jQuery plugins trying to deal with this. I’d recommend using a-tools. I’ve used it in the past, and it works as advertised.