I add text to a textarea
with the code
document.getElementById("b").addEventListener("click",
function(e) {
e.preventDefault();
var text='test',txtarea;
// Problem
var t = document.getElementsByTagName('textarea');
for(var i =0 ;i<t.length;i++){
if(t[i]==document.activeElement) {txtarea=t[i]}
}
var scrollPos = txtarea.scrollTop;
var strPos = 0;
strPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0, strPos);
var back = (txtarea.value).substring(strPos, txtarea.value.length);
txtarea.value = front + text + back;
strPos = strPos + text.length;
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
txtarea.scrollTop = scrollPos;
}
);
I want to find the focused textarea
(one in which writing currently) in the page.
I created a loop to check which textarea
is focused, but it does not work probably because when I click the focus shifts from the textarea
to a
element.